我怎樣才能在下面的html中對所有以 "label style-scope ytcp-button "開頭的行執行 "點擊 "動作呢?
<div class="label style-scope ytcp-button" > Edit Draft< /div>
這是我嘗試的結果
$("div[class^=label style-scope ytcp-button]").click()。
它似乎不作業。
uj5u.com熱心網友回復:
你可以使用簡單的JS方法querySelectorAll()。
const els = document. querySelectorAll('.label.style-scope.ytcp-button') 。
els.forEach((el) => {
el.click()。
console.log('clicked')。
});
uj5u.com熱心網友回復:
你漏掉了撇號。你應該這樣寫:-
$("div[class^='label style-scope ytcp-button']").click()。
運行下面的代碼片段,它不會在點擊最后一個div時運行alert方法,因為最后一個div中缺少"ytcp-button"類。
。$(document)。 ready(function() {
$("div[class^='label style-scope ytcp-button']").click(function() {
alert("You have Click a Specific Class") 。
});
});
<!DOCTYPE html>
<html lang="en">
<head>/span>
<meta charset="UTF-8">/span>
< meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=devicewidth, initial-scale=1. 0">
<title>Document</title>
</head>/span>
<body>
<div class="label style-scope ytcp-button"> 它將顯示警報</div>
<div class="標簽樣式-范圍 ytcp-按鈕"> 它將顯示警報</div>
<div class="標簽樣式-范圍 ytcp-按鈕"> 它將顯示警報</div>
<div class="標簽樣式范圍"> 它不會顯示警報</div>。
<script src="https://code.jquery.com/jquery-3.6.0.min. js" integrity="sha256- /xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"/span>> </script>>
<script src="script. js"></script>>
</body>
</html>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331513.html
標籤:
