最近,我一直在处理用于在单击 3 次后替换元素的 javascript 代码,我的问题是,我无法获得 data-id-slot-""
。参数也被替换。对于这个元素 <ins data-ad-slot="4092520690">ins</ins>
.我所管理的只是单击 3 次后替换 innerText。
function replaceAfter3Clicks(elem, newElem) {
let count = 0;
let callback = function() {
count++;
if (count >= 3) {
elem.replaceWith(newElem);
}
}
ins1.click(callback);
}
const ins1 = $("ins[data-ad-slot]");
// pre-made second ins for future replacement
const ins2 = document.createElement('ins');
ins2.?? = '9020596432';
ins2.innerText = 'ins2';
replaceAfter3Clicks(ins1, ins2);
最佳答案
我认为您对 HTML 属性和 javascript 对象属性之间的区别感到困惑
ins2.?? = '9020596432';
我想你想要更像这样的东西
ins2.setAttribute("data-id-slot", "9020596432")
参见 https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
关于javascript - 需要帮助替换元素的特定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830046/