【问题标题】:How to get the path of tag if it has a space in its class or id如果标签的类或id中有空格,如何获取标签的路径
【发布时间】:2021-05-18 00:52:59
【问题描述】:

我正在尝试获取带有类名的标签的路径(使用cheerio)。我试过了

let title =$('div.group-flag > div >a').text(); 

这很好用。 我正在尝试获取text required

我试过了

let title=$('//span[starts-with(@class,"js-course-title")]') 

这给出了一个错误。

$('span[class="js-course-title.d-none.d-sm-block"]') 

这不返回任何值(或length=0)。请让我知道如何获得所需的文本?

【问题讨论】:

    标签: html jquery cheerio


    【解决方案1】:

    您应该更改您的班级和 id 名称以删除空格。

    id 不能包含空格。请参阅MDN on id“id 的值不得包含空格(空格、制表符等)。”

    与元素关联的类列表是一个空格分隔列表,因此在类名中包含空格是没有意义的。见MDN on classes

    【讨论】:

    • 您好,感谢您提供的文档,我仍然没有得到它可能是我的路径错误,例如我有 Bochum $('span[class="cadcarouselsubtitlecadcarouselsubtitlelocationcadcarouselsubtitlesmall"]') 你能告诉我路径应该是什么吗?
    【解决方案2】:

    要么

    $('[class="c-ad-carousel__subtitle c-ad-carousel__subtitle--location c-ad-carousel__subtitle--small"]')
    

    或者

    $('.c-ad-carousel__subtitle.c-ad-carousel__subtitle--location.c-ad-carousel__subtitle--small')
    

    【讨论】:

    • 感谢您的回答,但它不起作用。如果有其他组合,请告诉我
    • 如果它不起作用,则意味着它实际上并不存在。你应该检查html。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多