Jquery check element exists by data attribute matching only the start of a specific value [duplicate]

StackOverflow https://stackoverflow.com/questions/23344485

문제

I want to be able to check to see if a specific data attribute exists that starts with a specific value.

for example lets say i have the following HTML code:

 <div data-attr="123ABC"></div>
 <div data-attr="123456"><div>
 <div data-attr="TEST"></div> 

and then i want to find any use of the data tag of data-attr that starts with 123

 if ($('[data-attr="123"]').length > 0) { 
     //do something
 }

This appears to always return 0 as i have no data-attr="123" How would i make it so i can check for only that specific data attribute that starts with 123 and has anything after that?

도움이 되었습니까?

해결책

here is how

if ($('[data-attr^="123"]').length > 0) { 
 //do something
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top