[英]How can I get element by ClassName with starts with a certain string?
我需要以ClassName開頭的元素:
width-48 height-48 profile_view_img
完整標簽:
<div class="width-48 height-48 profile_view_img_9131256"></div>
此類的數字始終在變化。
到目前為止,這是我嘗試過的:
var x = window.document.querySelectorAll("*[class^=\"width-48 height-48 profile_view_img\"]")[0]
請嘗試以下操作:
var x = window.document.querySelectorAll("*[class^=\"width-48 height-48 profile_view_img\"]")[0]
var className = x.className;
JSFiddle 。
您可以像這樣使用querySelector
:
var element = document.querySelector(".width-48.height-48.profile_view_img");
或者只使用getELementsWithClass
接受這樣的空格:
var elements = document.getElementsByClassName("width-48 height-48 profile_view_img");
你快到了。
嘗試:
var x = document.querySelectorAll('.width-48.height-48.profile_view_img')[0];
內容為:
假設變量X為包含類
.width-48
AND.height-48
AND.profile_view_img
的文檔中的第一個元素 。
對於第一個具有名稱以開頭的類的元素
width-48
和height-48
以及profile_view_img
您可以嘗試:
var x = window.document.querySelectorAll("*[class^='width-48'] *[class^='height-48'] *[class^='profile_view_img']")[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.