我堅持這一點,需要一些幫助。我知道這個邏輯解決方案,但不知道如何將它翻譯成jQuery代碼(新手在這裏:))。任何建議或幫助非常感謝!jQuery的addClass元素基於屬性值
我需要添加一個css類(活動類)到一個標題屬性等於變量值的元素。所以,讓我們說我的變量currentElement ='偉大的一天'。頁面上還有一個標籤元素,其標題屬性爲「偉大的一天」,例如<a title='great day' href='#'>
。
用jQuery我想:在DOM中找到一個標籤currentElement ==標籤標題並添加css類。所以,理想情況下它應該找到一個標籤並將css類添加到它。
我想出了這樣的事情,但不工作:
/* set currentElement var to the src attribute
value of an a tag on the page
this works */
var currentElement = $('#greatLink'] img[src*=' + test + ']').attr('src');
/* now find an a tag on the page that has
the same title attribute as var currentElement
this does not work */
$("a[title ='+currentElement+']").addClass('selectedThumb');
示例代碼中的一些語法錯誤(*第一部分*) – 2010-12-18 22:47:31