gpt4 book ai didi

javascript - 通过 JavaScript 在 ins 标签内多次添加值

转载 作者:行者123 更新时间:2023-12-04 16:15:53 26 4
gpt4 key购买 nike

我有这个代码~

<!-- ONE -->
<ins class='adsbygoogle'
style='display:block'
data-ad-client='ca-pub-XXXXXXXXXXXXXXX'
data-ad-format='auto'
data-full-width-responsive='true'></ins>

<!-- TWO -->

<ins class='adsbygoogle'
style='display:block'
data-ad-client='ca-pub-XXXXXXXXXXXXXXX'
data-ad-format='auto'
data-full-width-responsive='true'></ins>
(相同的代码,两次)
我用它来添加一个值“data-ad-slot=123456789”
<script> 
var x= 123456789;
var elem = document.querySelector('.adsbygoogle');
elem.dataset.adSlot = x;
console.log(elem);
</script>
但它只与第一个( <!--ONE--> )一起工作一次。如何使它与多个代码一起工作?
谢谢!

最佳答案

请试试这个,你将不得不使用 querySelectorAll,它会返回一个列表。

<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<!-- ONE -->
<ins class='adsbygoogle' style='display:block' data-ad-client='ca-pub-XXXXXXXXXXXXXXX' data-ad-format='auto'
data-full-width-responsive='true'></ins>

<!-- TWO -->
<ins class='adsbygoogle' style='display:block' data-ad-client='ca-pub-XXXXXXXXXXXXXXX' data-ad-format='auto'
data-full-width-responsive='true'></ins>
<script>
window.addEventListener('load', (event) => {
var x = 123456789;
var elems = document.querySelectorAll('.adsbygoogle');
elems.forEach((elem) => {
elem.dataset.adSlot = x;
console.log(elem);
});
});
</script>
</body>
</html>
输出:
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXXXXXXXXXXX" data-ad-format="auto" data-full-width-responsive="true" data-ad-slot="123456789"></ins>

<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXXXXXXXXXXX" data-ad-format="auto" data-full-width-responsive="true" data-ad-slot="123456789"></ins>

关于javascript - 通过 JavaScript 在 ins 标签内多次添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63219586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com