简体   繁体   English

document.querySelectorAll()的多个值

[英]document.querySelectorAll() of multiple values

I have this code: 我有这个代码:

<a href="javascript:alert('something1')">Click</a>
<a href="javascript:prompt('something2')">Click</a>
<a href="javascript:alert('something3')">Click</a>
<a href="javascript:prompt('something4')">Click</a>

To get individual set element, I use document.querySelectorAll("a[href^='javascript:alert("); and document.querySelectorAll("a[href^='javascript:prompt"); . 为了获得单个set元素,我使用document.querySelectorAll("a[href^='javascript:alert(");document.querySelectorAll("a[href^='javascript:prompt");

Now, how can use a generic document.querySelectorAll() to get all such <a href elements which contains alert, and prompt? 现在,如何使用通用document.querySelectorAll()来获取包含alert和提示符的所有<a href元素?

I tried this: 我试过这个:

document.querySelectorAll("a[href^='javascript:prompt(,a[href^='javascript:alert(");

document.querySelectorAll("a[href^='javascript:prompt(","a[href^='javascript:alert(");

and so many. 这么多 But it doesn't work, got "DOMException - not a valid selector" error. 但它不起作用,得到"DOMException - not a valid selector"错误。

Any help? 有帮助吗?

Your selector is not valid 您的选择器无效

  1. Missing closing quote of attribute value( ' ). 缺少属性值( ' )的结束引用。
  2. Missing ending of attribute selector ] . 缺少属性选择器的结尾]
  3. Also use querySelectorAll to get NodeList, querySelector only return single element . 还使用querySelectorAll获取NodeList, querySelector只返回单个元素。

 console.log( document.querySelectorAll("a[href^='javascript:prompt('],a[href^='javascript:alert(']") //----------------------------------------------------^^---------------------------^^^ ); 
 <a href="javascript:alert('something1')">Click</a> <a href="javascript:prompt('something2')">Click</a> <a href="javascript:alert('something3')">Click</a> <a href="javascript:prompt('something4')">Click</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM