原代码,查找所有选择器,并根据文本选中元素并点击
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 年 3 月 6 日 不是 text 变量大小写搞错了?
|
3
ysc3839 2022 年 3 月 6 日 via Android
“单引号必须在函数中才行”具体指的是什么?
|
5
KobeSama 2022 年 3 月 6 日
"'1'"|| '\'1\''
`'1'` || `'${text}'` |
6
ragnaroks 2022 年 3 月 6 日
#1 正解
function findSelectorTextClick(selector, text) { Array.from(document.querySelectorAll(selector)) .find(el => el.textContent === text) .click(); } findSelectorTextClick('.tag', ' 传参') |
9
ljsh093 2022 年 3 月 6 日 via iPhone
用``可以随便传
|