为什么 jQuery 没有选择所有属性元素?
Why is jQuery not selecting all attribute elements?
我在网站上制作标签,我设置了 jQuery 到 运行 功能,当任何属性被点击时(这只是为了测试选择器,并不是所有的属性都会被用到最终网站)。 None的属性会在我进入某个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 到 运行 功能,当任何属性被点击时(这只是为了测试选择器,并不是所有的属性都会被用到最终网站)。 None的属性会在我进入某个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
});