问题与document.querySelectorAll;a [href =]'不是有效的选择器 [英] Problem with document.querySelectorAll; a[href=]' is not a valid selector

查看:79
本文介绍了问题与document.querySelectorAll;a [href =]'不是有效的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法搜索文档

I'm trying to search the document using:

document.querySelectorAll("a[href*=google.com]")[0].href;

要在文档中搜索包含google.com网址的href奇怪的是,到目前为止,它一直对我有用会发生什么事?这是向我显示的错误:

To search the document for a href containing the google.com URL The strange thing is that it has always worked for me to this day What will have happened? This is the error that shows me:

Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': 'a[href=google.com]' is not a valid selector.
at <anonymous>:1:10

我再说一遍,我已经使用该代码很多年了,今天它已经停止工作,请问任何解决方案吗?

I repeat, I have been using that code for years and today it has stopped working, any solution please?

这是我的HTML:

<html>
    <head>
        <title> MY WEB </title>
    </head>
    <body>
        <a rel="nofollow" href="//www.google.com/" target="_blank">GOOGLE</a>
    </body>
</html>

推荐答案

在Google.com值中添加引号

Add quote '' to google.com value

let result = document.querySelectorAll("a[href*='google.com']")[0].href;

let result = document.querySelectorAll("a[href*='google.com']")[0].href;
console.log(result)

<html>
<head>
<title> MY WEB </title>
</head>
<body>
<a rel="nofollow" href="//www.google.com/" target="_blank">GOOGLE</a>
</body>
</html>

这篇关于问题与document.querySelectorAll;a [href =]'不是有效的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆