jQuery-将onclick添加到TR

jQuery-将onclick添加到TR,jquery,jsf,richfaces,Jquery,Jsf,Richfaces,我使用Richfaces,其中一个rich:dataTable生成以下html: <form id="scheduleForm"> <table id="scheduleForm:rowList"> <tr> <td></td> </tr> <tr> <td></td> </tr> <tr>

我使用Richfaces,其中一个rich:dataTable生成以下html:

<form id="scheduleForm">
  <table id="scheduleForm:rowList">
    <tr>
      <td></td>
    </tr>
    <tr>
      <td></td>
    </tr>
    <tr>
      <td></td>
    </tr>
  </table>
</form>
关于“:”字符。它是由Richfaces生成的

原代码为:

<h:form id="scheduleForm">
<rich:dataTable id="rowList">
</rich:dataTable>
</h:form>

您可以执行以下操作,给tr一个id

<form id="scheduleForm">
  <table id="scheduleForm:rowList">
    <tr id="tr1">
      <td></td>
    </tr>
  </table>
</form>

jQuery('#tr1').click(function(event) {
    alert(1);
});

jQuery('#tr1')。单击(函数(事件){
警报(1);
});

尝试以下方法,它会对您有所帮助

jQuery(document.getElementById('scheduleForm:rowList').getElementsByTagName('tr')).click(function(event) {
          alert(1);
        });
尝试使用:

jQuery('#scheduleForm:rowList>tr').click(function(event) {
            alert(1);
        });

最好不要使用charter:“在id属性的值中。我不知道确切的<,但我认为这可能是许多问题的原因。

我知道
是由JSF/RichFaces生成的。我以前也遇到过这样的问题:。这可能不是jQuery人员对ID的期望。他们将其用于选择器。您可能想做的是:

jQuery('table[id = "scheduleForm:rowList"] tr').click(...)

签出ID属性中有一个冒号
。冒号在jQuery选择器中有一个特殊的含义,因此您需要像这样对其进行转义:

jQuery('#scheduleForm\\:rowList tr').click(function(event) {
      alert(1);
});

:由JSF生成。 您还可以在表单上设置prependId=“false”。然后使用:

jQuery('#rowList tr').click(function(event) { alert('hello'); });

并且只会选择该表

我想“:”可能在这里玩一些东西。如果我没有错的话“:”是一些元字符,在选择器中有一些含义。这是显而易见的,但我不认为用JSF和RichFaces在上强制id是非常简单的……谢谢,我忘了提到可以有更多行。我希望该表的所有行都使用它。虽然这段代码可以回答这个问题,但提供关于它如何和/或为什么解决这个问题的附加上下文将提高答案的长期价值。
jQuery("#rowList").on("click", "tr", function(event) { 
    alert($(this).id); 
});
jQuery("#rowList").on("click", "tr", function(event) { 
    alert($(this).id); 
});