给定嵌套的 html:
<a class="star">Hello world!
<a class="ui-icon-star"> hello </a>
</a>
我使用了以下 JS:
$('.star').click(function () {
$(this).toggleClass("favorited");
$(this > '.ui-icon-star').toggleClass("ui-icon-starred");
});
更改 .star 和子 .ui-icon-star 元素,但它不适用于子元素。我的 [selector(?)] 错误在哪里?我猜大约是 $(this > '.ui-icon-star')。
1:http://jsfiddle.net/tNwND/1/ 2: http://www.w3.org/TR/CSS21/selector.html
最佳答案
那是因为你的标记无效,浏览器以这种方式呈现你的标记:
<a class="star">Hello world! </a>
<a class="ui-icon-star"> hello </a>
所以被点击的元素没有.ui-icon-star
子元素。
关于javascript - JQuery & 子选择器 A > B : not working ? [Chrome 24, JQmobile],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14863982/