javascript - JQuery & 子选择器 A > B : not working ? [Chrome 24, JQmobile]

标签 javascript jquery jquery-mobile jquery-selectors

给定嵌套的 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/

相关文章:

javascript - 将 JavaScript/WebGL 移植到 Python/OpenGL

javascript - 使用 Cheerio 解析表格单元格内 HTML 表单中的文本

javascript - 使用ajax动态更新表格行的值

javascript - 特定页面请求操作的音频持续时间 NaN

javascript - 如何向动态创建的元素触发事件?

javascript - libphonenumber javascript - 验证

javascript - 在 .mousemove 函数中循环

javascript - jQuery Mobile 转换根本不起作用

javascript - 包含多个元素的公式

jquery - 无法通过ajax更改按钮值