2014-03-12 90 views
0

我在页面上有以下JQuery。JQuery适用于页面加载,但不适用于

$("." + clazz).on("mousedown", function() { 
    $(this).removeClass(clazz); 
    $(this).addClass(clazzPressed); 
}); 
$("." + clazz).on("mouseup", function() { 
    $(this).removeClass("clazzPressed"); 
    $(this).addClass("clazz"); 
    setCookie('cmpaid',$(this).attr('id'),1); 
    divClickAction(); 
}); 

这对我来说工作得很好。但是,我使用一些AJAX并更新页面上的某些内容。更新发生后,此代码不再有效。

我怀疑它只能在页面加载时调用它。一旦我更新我的页面,新的东西不知道它。有没有更新新的东西来拥有这个JQuery代码的方法?

如果它的事项,这是被通过Ajax更新代码:

<h:panelGroup id="urlList" > 
       <script src="/resources/scripts/divClick.js"/> 
      <script src="/resources/scripts/cookie.js"/> 
       <ui:repeat id="urlRepeater" value="#{uRLManagerManagedBean.urls}" var="url" > 
        <div id="#{url.id_value.toString()}" class="divClick" style="margin-bottom: 10px;margin-left: 15px;">    
         <div style="float:right;"> 
          <p:outputLabel value="#{url.category.category}" /><br/> 
          <p:outputLabel value="Embedded" rendered="#{url.isEmbedded}"/> 
         </div> 
         <p:graphicImage value="#{streamedContentManagedBean.image}" styleClass="urlImageSize" style="float: left;min-width: 25px;"> 
          <f:param name="id" value="#{url.image.idAsString}" /> 
         </p:graphicImage> 
         <h:panelGrid columns="1" > 
          <p:outputLabel value="#{url.name}" /> 
          <p:outputLabel value="#{url.url}" rendered="#{not url.isEmbedded}" /> 
          <p:outputLabel value="#{url.embed.url}" rendered="#{url.isEmbedded}" /> 
          <p:outputLabel value="#{url.type.toString(url.type)}" /> 
          <h:outputText value="#{url.description}" escape="false"/> 
         </h:panelGrid> 
        </div> 
       </ui:repeat> 

       <h:panelGroup layout="block" class="cardBody" style="margin-bottom: 10px;margin-left: 15px;padding:10px 10px 10px 10px;" rendered="#{empty uRLManagerManagedBean.urls}" > 
        <h:outputLabel value="No Urls" style="font-size: 25px;color: white;"/> 
       </h:panelGroup> 
      </h:panelGroup> 

这是JSF,并得到一些代码后进行更新。但是这个问题应该是独立的。

+0

你可以包含jQuery脚本不起作用的页面源代码吗?或者这个脚本在/resources/scripts/divClick.js和/resources/scripts/cookie.js里面? – trims

回答

1

将您的JavaScript代码放入函数中,然后在页面加载时调用该函数。

那么对于preRenderView事件页面上注册一个监听器:

<f:metadata> 
    <f:event type="preRenderView" listener="#{backingBean.preRenderViewListener}"/> 
</f:metadata> 

在监听方法:

public void preRenderViewListener() 
{ 
    RequestContext.getCurrentInstance().execute("someJavaScriptFunction()"); 
} 

中的JavaScript将每个Ajax请求之后再次调用。

+0

真棒,让我试试这个 – dan

相关问题