我正在尝试从网站获取“a”标签的值。我在 Google Chrome 的控制台中运行下面的代码。
document.querySelector('#container > article:nth-child(1) > div > h1 > a');
执行返回这个标签:
<a class="name-link" href="/shop/blarg/foo/bar">blarg</a>
我正在尝试返回“blarg”,最好是在变量中。我尝试了以下方法:
document.querySelector('#container > article:nth-child(1) > div > h1 > a').value;
然而它只是返回'undefined'。任何帮助将不胜感激,谢谢
最佳答案
你应该选择 innerHTML
document.querySelector('#container > article:nth-child(1) > div > h1 > a').innerHTML;
您也可以像这样编辑该 html
document.querySelector('#container > article:nth-child(1) > div > h1 > a').innerHTML="new value";
关于javascript - 使用 document.querySelector 从网站获取值/名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54923814/