這裏是一個單選按鈕幫助包括()
<input type="radio" class="grgrgrtgxxxxfeegrgbrg">
我試圖檢查單選按鈕的名稱是否有XX的HTML代碼。
這裏是我的jQuery代碼我已經寫
if($(this).prop('class').contains('xxxx'))
{
alert("Yup");
}
請告訴我它的語法錯誤。
這裏是一個單選按鈕幫助包括()
<input type="radio" class="grgrgrtgxxxxfeegrgbrg">
我試圖檢查單選按鈕的名稱是否有XX的HTML代碼。
這裏是我的jQuery代碼我已經寫
if($(this).prop('class').contains('xxxx'))
{
alert("Yup");
}
請告訴我它的語法錯誤。
contains
是一個jQuery方法與要素的作品不是字符串,所以你必須使用字符串方法:
if($(this).prop('class').indexOf("xx")>-1)
{
alert("Yup");
}
但正如別人說,你可以使用hasClass:
if($(this).hasClass("xx")) ...
contains
ISN」在JavaScript中使用字符串的方法。因爲從你的問題你真的想在一個類名稱的中間串匹配,你可以使用indexOf
:
if ($(this).prop('class').indexOf('xx') >= 0)
{
alert("Yup");
}
請注意,您不需要jQuery的這所有,只是使用reflected property的DOM元素:
if (this.className.indexOf('xx') >= 0)
{
alert("Yup");
}
className
反映"class"
屬性。
爲什麼不使用.hasClass()呢? –
'prop'不會返回一個jQuery對象。 –