使用数据attr
创建一个元素 <div class="component" data-bind="component: { name: "recently-viewed"}">
现在我想使用JS
选择它:
document.querySelector('div[data-bind*="component: { name: \"recently-viewed\""]')
如何让它返回元素?我无法编辑标记。
答案 0 :(得分:1)
切换你的报价:
console.log(document.querySelector('div[data-bind*="component: { name: \'recently-viewed\'}"]'))
<div class="component" data-bind="component: { name: 'recently-viewed'}">
document.querySelector('div[data-bind*="component: { name: \'recently-viewed\'"]')
答案 1 :(得分:0)
您也可以使用其他迷你语录
document.querySelector(`div[data-bind*='component: { name: "recently-viewed"']`)