
原代码,查找所有选择器,并根据文本选中元素并点击
Array.from(document.querySelectorAll('.class')) .find(el => el.textCOntent=== '1') .click(); 想写成函数复用,发现传参的时候单引号必须在函数中才行 请问如何解决 谢谢
function findSelectorTextClick(selector, text) { Array.from(document.querySelectorAll(selector)) .find(el => el.textCOntent=== Text) .click(); } findSelectorTextClick('.class', '1') 1 wudicgi 2022-03-06 02:08:44 +08:00 不是 text 变量大小写搞错了? |
3 ysc3839 2022-03-06 02:16:08 +08:00 via Android “单引号必须在函数中才行”具体指的是什么? |
5 KobeSama 2022-03-06 05:52:48 +08:00 "'1'"|| '\'1\'' `'1'` || `'${text}'` |
6 ragnaroks 2022-03-06 09:41:23 +08:00 #1 正解 function findSelectorTextClick(selector, text) { Array.from(document.querySelectorAll(selector)) .find(el => el.textCOntent=== text) .click(); } findSelectorTextClick('.tag', ' 传参') |
9 ljsh093 2022-03-06 14:54:44 +08:00 via iPhone 用``可以随便传 |