jquery - 为什么 jQuery 不选择所有属性元素?

标签 jquery html css jquery-selectors attributes

我在网站上制作标签,我设置 jQuery 以在单击任何属性时运行函数(这只是为了测试选择器,并非所有属性都将在最终网站中使用)。进入某个div后,不会选择任何属性。 这是 HTML:

<div id="content" class="content">
     <div id="image" style="border:2px solid;float:left">...</div>
     <div id="backgroundInfo" style="margin-left: 5px;float:left">...</div>
     <br>
     <div id="medBackground" style="margin-left: 5px">...</div>
     <div id="tabs" class="tabs">
         <a> Clowns</a>
         <ul class="tab-links">
              <li class="active"><a href="#tab1">03/05/2001</a></li>
              <li><a href="#tab2">06/05/2007</a></li>
         </ul>
         <div class="tab-content">...</div>
     </div>
</div>

列表是使用 while 循环生成的。除了 div(class = tabs) 中的属性外,所有属性都被选中。 这是 jQuery:

jQuery(document).ready(function() {
    jQuery('a').on('click', function(e)  {
        var currentAttrValue = jQuery(this).attr('href');

        // Show/Hide Tabs
        jQuery('.tabs ' + currentAttrValue).show().siblings().hide();

        // Change/remove current tab to active
                jQuery(this).parent('li').addClass('active').siblings().removeClass('active');

        e.preventDefault();
    });
});

我从以下位置获得此代码:http://inspirationalpixels.com/tutorials/creating-tabs-with-html-css-and-jquery#step-jquery 如果有人能帮我弄清楚出了什么问题,那就太棒了。我让它以简化的形式工作,但不是在我的主网站上。

最佳答案

我认为您对动态生成的列表使用了错误的 .on() 方法, 尝试

jQuery(document).on('click', 'a', function(e){
    //your code
});

代替

 jQuery('a').on('click', function(e)  {
     //your code
 });

关于jquery - 为什么 jQuery 不选择所有属性元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246694/

相关文章:

jquery - 使用 overlayScrollbars.js 时 div 宽度自动折叠

javascript - 不同部分的相同功能 - jquery 中的相对引用

javascript - 如何获取单击按钮的标题属性?

javascript - AngularJs 清理和显示 html

css - 下划线超链接 - 文本装饰与边框底部和浏览器不一致

javascript - 元素没有出现在包装器中,它们在下面

jquery - 一张一张地填充不同宽度和高度的瓷砖

JQuery ui 可调整大小未达到最大宽度

javascript - fps 保存在 html5 视频的元数据中的什么位置?

css - 为什么 flexbox 会影响其包含列表项的标记?