【问题标题】:How to select HTML elements whose type has a dash in them如何选择类型中有破折号的 HTML 元素
【发布时间】:2015-12-09 13:42:21
【问题描述】:

$('foo\\-bar') 有效。

我想做$('foo\\-bar', '<foo-bar>foobar</foo-bar>'),但这行不通。为什么?以及如何在我的字符串中选择所有foo-bar 标签?

【问题讨论】:

    标签: jquery jquery-selectors css-selectors


    【解决方案1】:

    你不需要逃避破折号。只需使用

    $("foo-bar")
    

    document.querySelector("foo-bar")
    

    $('foo\\-bar', '<foo-bar>foobar</foo-bar>') 不起作用,因为上下文(第二个参数)也是上下文中的根元素,这就是为什么 foo-bar 不能作为上下文元素/字符串的子元素找到的原因。

    另一方面,这将起作用:

    $("foo-bar", "<div><foo-bar></foo-bar></div>")
    

    【讨论】:

    • 谢谢 :) 但是为什么 $("work-title", "tada") 不起作用?
    • 我不知道。它在 Firefox 40 上对我有用。
    • 我的代码与 jQuery 发生冲突...问题解决了!
    猜你喜欢
    • 2011-03-13
    • 2020-09-01
    • 1970-01-01
    • 2013-12-02
    • 2011-11-25
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多