【问题标题】:CSS Selectors attribute not selecting [duplicate]CSS选择器属性未选择[重复]
【发布时间】:2021-02-17 14:38:39
【问题描述】:

如何使用 CSS 选择器选择 "meta property="og:title""

<meta property="og:type" content="article" />
<meta property="og:title" content="How Computers Got to Where They are" />
<meta property="og:description" content="In the late 1950s, Goyeman forever changed the theory of computing when he discovered atoms interacting subparts of dinucleau particles to lead to an incredible journey." />
<meta property="og:url" content="https://www.voltamagazine.org/how-goyeman-revolutionized-computing-21380521/" />
<meta property="og:site_name" content="Volta Magazine" />
<meta property="og:image" content="https://y2655xcwy6xx58.cloudfront.net/uploads/2016/01/Goyeman_1200x630-Social02.jpg" />
<meta property="og:image:width" content="1200" />

【问题讨论】:

    标签: html css css-selectors


    【解决方案1】:

    使用属性选择器[] 应该可以解决问题。。在此处查看ref

    document.querySelector("meta[property='og:title']")
    
    //for grabbing the attribute value
    const title = document.querySelector("meta[property='og:title']").content
    

    添加通配符赋予选择器权力。

    1. 从“og”开始选择属性
    document.querySelectorAll("meta[property^='og']")
    
    1. 以 og 结尾
    document.querySelectorAll("meta[property$='og']")
    
    1. 包含“og”
    document.querySelectorAll("meta[property*='og']")
    

    【讨论】:

    • 谢谢,是javascript还是css?
    • js和css都可以用。相同的机制。如果在 css 中使用,只需将其取出 meta[property='og:type']{//your css rules}
    • 谢谢,有道理,是否可以只选择“内容”以及 "How Computers Got to Where They are" 使用 css ?
    • 你的用例是什么?
    • 抓取这个字符串并保存。我正在使用需要 CSS 选择器的网络爬虫来执行此操作。
    猜你喜欢
    • 2016-01-13
    • 2018-08-19
    • 2010-10-19
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2012-10-05
    相关资源
    最近更新 更多