問題就像下面這樣,當我嘗試選擇一個以關鍵字開頭並以另一個關鍵字結束的類時,很好,當且僅當元素有一個類時,如果元素有多個類,選擇器將返回一個空集合。jQuery selector:'starts&with&ends-with'does not work on element with multiple classes
這裏是代碼說明問題
// try removing custom-class from first element --> returns 2
alert($("div[class^='start'][class*='end']").length) // will return 1 by default , only 1 element has single class.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-class start-mid-end" data-custom="1st Div">
</div>
<div class="start-mid-end" data-custom="2nd Div">
</div>
感謝您的回答,第二個答案會做,首先不會,但是當您正在研究一個非常古老而複雜的結構時,有時您沒有太多選擇。 – ProllyGeek