jquery - 点击后会显示具体内容

标签 jquery

我有一些 HTML

<div class="view-content">
   <table class="views-table cols-10" style="font-size: 12px;">
     <thead>
       <tbody>       
          <tr class="even" style="background-color: rgb(255, 255, 255);">
              <td class="views-field views-field-title" style="background-color: rgb(248, 248, 248); color: black;">
                   <b>Is there any fees charged to lodge the complaint?</b>
                   <p style="font-size: 12px; display: none;"> 
              </td>
        </tr>
        <tr class="odd" style="background-color: rgb(248, 248, 248);">
            <td class="views-field views-field-rownumber" style="background-color: rgb(248, 248, 248); color: black;"> 3 </td>
            <td class="views-field views-field-title" style="background-color: rgb(248, 248, 248); color: black;">
               <b>Can I lodge a complaint on behalf of another person?</b>
                     <p style="font-size: 12px; display: none;">
            </td>
       </tr>

我添加了jquery函数

$(document).ready(function() {
    $('.view-content').find('table tbody tr').each(function() {
        $(this).find('td.views-field-title p').hide();
        // alert($(this).html());
    });
})

问题是当我添加时:

$('td.views-field-title b').click(function () { 
      $('td.views-field-title p').show();
}) 

不起作用,该操作将显示 <p></p> 的所有内容 而是显示 <p></p>对于选定的'td.views-field-title b' .

有什么想法吗?

最佳答案

发生这种情况是因为您的 td.view-field-title p选择器匹配所有 <p> <td class="view-field-title"> 中的元素元素。我想你想使用 next 限制您显示的内容:

$('td.views-field-title b').click(function () {
    $(this).next('p').show();
});

不过要小心这种方法,它对 HTML 的结构非常敏感。更好的方法是返回 <td>然后归结为 <p>使用 closest find :

$('td.views-field-title b').click(function () {
    $(this).closest('td').find('p').show();
});

这仍然对您的 HTML 结构敏感,但不太敏感。

第二种方法的示例(修复了 HTML 错误):http://jsfiddle.net/ambiguous/CBLtt/

<小时/>

如果您只想一次只打开一个段落,那么只需对点击处理程序进行简单的修改即可:

$('td.views-field-title b').click(function () {
    $('td.views-field-title p').hide();      // Hide them all.
    $(this).closest('td').find('p').show();  // Then show the one we want.
});

示例:http://jsfiddle.net/ambiguous/CBLtt/1/

关于jquery - 点击后会显示具体内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6301693/

相关文章:

javascript - 如何动态调用事件处理程序并在 javascript 中获取其返回值?

javascript - 访问 javascript 中内联函数内所做的变量修改

jquery - 在单个页面上嵌入多个YouTube播放列表供稿? JQuery/YouTube API v3

jquery - 如何将 ZeroClipboard 剪辑粘到新插入的元素上?

javascript - jQuery 对象和参数 - 使用正确的术语/解释

jquery - 如何在 ASP.NET 表中创建 thead 和 tbody ?

javascript - jQuery如何通过不同的列值计算表中的行数

javascript - 如何为 imgProgress.js 插件创建动画?

jquery - 如何通过 AJAX 将表单数据提交到 Braintree 透明重定向?

javascript - 升级.net框架后,Shield ui图表消失