![](/img/trans.png)
[英]get value inside DOM setAttribute JavaScript using BeautifulSoup
[英](javaScript: DOM) setAttribute index
我正在嘗試通過 javaScript DOM 在<img>
中添加圖像,只是想知道為什么第一個代碼有效但第二個代碼無效? 為什么img
需要索引?
var img = document.querySelectorAll("img");
img[0].setAttribute("src", "images/image_1.jpg");
img[0].setAttribute("style", "width:500px; height:200px;");
var img = document.querySelectorAll("img");
img.setAttribute("src", "images/image_1.jpg");
img.setAttribute("style", "width:500px; height:200px;");
因為document.querySelectorAll
返回一個元素列表。
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
如果您改用document.querySelector
,則無需指定索引,因為它返回文檔中與指定選擇器匹配的第一個元素。
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.