jquery 选择器在带有自定义属性(knockoutjs)的 td 上的 IE8 中无法在 document.ready 中工作

发布于 2025-01-01 07:10:47 字数 6084 浏览 1 评论 0原文

当这些元素具有自定义属性时,我无法让 jquery 按 id 或 class 选择元素...(在本例中,元素是具有属性“data-bind”的 td 标签 - 使用 KnockoutJS)

如果我选择标签,例如 $("table tr") ,它工作正常。如果我将 runat="server" 添加到表标记,然后将选择器更改为 $("#<%=tblTimslips.ClientID%>"),但如果我这样做,那么淘汰赛将不再识别该表。

编辑:我添加了另一个选择器(下面的 javascript 中未注释的选择器),它成功地获取了 THEAD 标签中的 TR,但没有获取 TBODY 标签中的 TR。

为什么它不按 ID 或类别进行选择?

<style type="text/css">
    .highlight2
    {
        background-color: #0000ee;
    }
</style>
<script type="text/javascript"> 

    $(document).ready(function () {
        loadClient();
        //$(".TimeslipsGrid tr").hover(function () { 
        // above selector by class doesn't work, nor does the following by id:
        // $("#tblTimeslips").hover(function(){     
        //  however, the following selector WILL work on all tr on the page, including my targets in #tblTimeslip:
        // $("table tr").hover(function(){
        // the following selector works, but ONLY FOR THE TR CONTAINED IN THEAD..
        //  changing it to "#tblTimeslips tbody tr" doesn't help, either..
        $("#tblTimeslips tr").hover(function() {
            $(this).addClass("highlight2");
        }, function () {
            $(this).removeClass("highlight2");
        })
</script>

<table id="tblTimeslips" class="TimeslipsGrid">
    <thead>
    <tr>
        <th>Time</th><th>Dur</th><th>File</th><th>Task/Activity</th><th>Note</th>
    </tr>
    </thead>
    <tbody data-bind="foreach: timeslips">
    <tr>
        <td data-bind="text: TimeString"></td>
        <td data-bind="text: DurationString"></td>
        <td data-bind="text: FileNumber"></td>
        <td data-bind="text: TaskActivityAbbrText"></td>
        <td><img data-bind="attr: { src: NoteIconPath }" /></td>
    </tr>
    </tbody>
</table>

这是 #tblTimeslips html 在淘汰赛完成后的样子。我还是看不出问题所在。

<table class="TimeslipsGrid" id="tblTimeslips">
<THEAD>
<TR jQuery17108028844206682706="72">
<TH>Time</TH>
<TH>Dur</TH>
<TH>File</TH>
<TH>Task/Activity</TH>
<TH>Note</TH></TR></THEAD>
<TBODY data-bind="foreach: timeslips" __ko__1328033964035="ko1">
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko2">
<TD data-bind="text: TimeString" __ko__1328033964035="ko3">00:00<BR>06:23 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko4">6.38</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko5">9-12632-555</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko6">Admin | Agnt Suprvsn</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko7"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko8">
<TD data-bind="text: TimeString" __ko__1328033964035="ko9">06:23<BR>06:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko10">0.08</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko11">9-12601-7</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko12">Admin | PTOW</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko13"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko14">
<TD data-bind="text: TimeString" __ko__1328033964035="ko15">06:28<BR>08:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko16">2</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko17">(no timeslip)</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko18">(none)</TD>
<TD><IMG src="../Images/Homepage/note_empty_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko19"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko20">
<TD data-bind="text: TimeString" __ko__1328033964035="ko21">08:28<BR>09:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko22">1</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko23">9-12632-553</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko24">T-A | KM</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko25"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko26">
<TD data-bind="text: TimeString" __ko__1328033964035="ko27">09:28<BR>11:01 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko28">1.55</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko29">(no timeslip)</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko30">(none)</TD>
<TD><IMG src="../Images/Homepage/note_empty_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko31"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko32">
<TD data-bind="text: TimeString" __ko__1328033964035="ko33">11:01<BR>10:00 PM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko34">10.98</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko35">2-9999-1</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko36">ATL | Doc Conv &amp;DE</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko37"></TD></TR></TBODY>
</table>

I can't get jquery to select elements by id or class when those elements have custom attributes on them... (in this case, the elements are td tags with attribute "data-bind" - am using knockoutJS)

If I select by tag, e.g. $("table tr") , it works fine. It also works if I add runat="server" to the table tag, and then change the selector to $("#<%=tblTimslips.ClientID%>"), but if I do that, then knockout doesn't recognize the table anymore.

Edit: I added another selector (the uncommented one in the javascript below), which succesfully grabs the TRs in the THEAD tag, but not the ones in the TBODY tag.... ??

Why won't it select by ID or class?

<style type="text/css">
    .highlight2
    {
        background-color: #0000ee;
    }
</style>
<script type="text/javascript"> 

    $(document).ready(function () {
        loadClient();
        //$(".TimeslipsGrid tr").hover(function () { 
        // above selector by class doesn't work, nor does the following by id:
        // $("#tblTimeslips").hover(function(){     
        //  however, the following selector WILL work on all tr on the page, including my targets in #tblTimeslip:
        // $("table tr").hover(function(){
        // the following selector works, but ONLY FOR THE TR CONTAINED IN THEAD..
        //  changing it to "#tblTimeslips tbody tr" doesn't help, either..
        $("#tblTimeslips tr").hover(function() {
            $(this).addClass("highlight2");
        }, function () {
            $(this).removeClass("highlight2");
        })
</script>

<table id="tblTimeslips" class="TimeslipsGrid">
    <thead>
    <tr>
        <th>Time</th><th>Dur</th><th>File</th><th>Task/Activity</th><th>Note</th>
    </tr>
    </thead>
    <tbody data-bind="foreach: timeslips">
    <tr>
        <td data-bind="text: TimeString"></td>
        <td data-bind="text: DurationString"></td>
        <td data-bind="text: FileNumber"></td>
        <td data-bind="text: TaskActivityAbbrText"></td>
        <td><img data-bind="attr: { src: NoteIconPath }" /></td>
    </tr>
    </tbody>
</table>

Here's what the #tblTimeslips html looks like after knockout is done with it. I still can't see the problem.

<table class="TimeslipsGrid" id="tblTimeslips">
<THEAD>
<TR jQuery17108028844206682706="72">
<TH>Time</TH>
<TH>Dur</TH>
<TH>File</TH>
<TH>Task/Activity</TH>
<TH>Note</TH></TR></THEAD>
<TBODY data-bind="foreach: timeslips" __ko__1328033964035="ko1">
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko2">
<TD data-bind="text: TimeString" __ko__1328033964035="ko3">00:00<BR>06:23 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko4">6.38</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko5">9-12632-555</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko6">Admin | Agnt Suprvsn</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko7"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko8">
<TD data-bind="text: TimeString" __ko__1328033964035="ko9">06:23<BR>06:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko10">0.08</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko11">9-12601-7</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko12">Admin | PTOW</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko13"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko14">
<TD data-bind="text: TimeString" __ko__1328033964035="ko15">06:28<BR>08:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko16">2</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko17">(no timeslip)</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko18">(none)</TD>
<TD><IMG src="../Images/Homepage/note_empty_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko19"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko20">
<TD data-bind="text: TimeString" __ko__1328033964035="ko21">08:28<BR>09:28 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko22">1</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko23">9-12632-553</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko24">T-A | KM</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko25"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko26">
<TD data-bind="text: TimeString" __ko__1328033964035="ko27">09:28<BR>11:01 AM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko28">1.55</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko29">(no timeslip)</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko30">(none)</TD>
<TD><IMG src="../Images/Homepage/note_empty_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko31"></TD></TR>
<TR jQuery17108028844206682706="73" __ko__1328033964035="ko32">
<TD data-bind="text: TimeString" __ko__1328033964035="ko33">11:01<BR>10:00 PM</TD>
<TD data-bind="text: DurationString" __ko__1328033964035="ko34">10.98</TD>
<TD data-bind="text: FileNumber" __ko__1328033964035="ko35">2-9999-1</TD>
<TD data-bind="text: TaskActivityAbbrText" __ko__1328033964035="ko36">ATL | Doc Conv &DE</TD>
<TD><IMG src="../Images/Homepage/note_full_16.png" data-bind="attr: { src: NoteIconPath }" __ko__1328033964035="ko37"></TD></TR></TBODY>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2025-01-08 07:10:47

您的CSS选择器 .highlight2 tr{} 应该是 .highlight2{} (或 tr.highlight2{}) ,否则选择器将不会'匹配。与错误的 jquery 无关;)

Your css-selector .highlight2 tr{} should be .highlight2{} (or tr.highlight2{}) , otherwise the selector won't match. Has nothing to do with faulty jquery;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文