2011-08-25 264 views
5

所以我有別人的舊代碼,我試圖恢復。我對jQuery不太熟悉,但是運算符指定了什麼?jQuery @運營商?

的代碼是:

v_button_format = $('#' + v_form_id).find('input[@name=button_format]').val(); 
v_content_type = $('#' + v_form_id).find('input[@name=content_type]').val(); 

我使用jQuery 1.3和它拋出 「未捕獲的異常:語法錯誤,無法識別的表達式:[@名稱= button_format]」 的錯誤。是否存在兼容性問題?

回答

13

這是一個屬性選擇器。

@是XPath主義即不再jQuery中使用。
此外,新版本的jQuery要求屬性值用引號引起來。

因此,你應該寫

$('#' + v_form_id).find('input[name="content_type"]').val(); 
+0

被@操作與當前的jQuery向後兼容? – powtac

+1

@powtac:我不相信。 – SLaks

+0

很少有話題,但對於這樣的事情是不是更好使用'document.getElementsByName'? –

3

這意味着attributeinput[@name=button_format]表示the input tag with the name attribute equal to button_format

但是,您需要刪除最近版本中的@和quote button_format。這意味着@不向後兼容。 So quoth the docs

注:在jQuery的1.3 [@attr]除去的樣式選擇器(它們先前在jQuery 1.2中建議使用)。只需從選擇器中刪除「@」符號,以便再次使用它們。

+0

感謝關於「@」的提示! – powtac

0
input[@name=button_format] 

意味着名稱屬性設置爲 'button_format' 的輸入字段,即:

<input name="button_format"> 
0
input[@name=button_format] 

這是舊選擇 type.please刪除,如果@如果使用jQuery最新版本 新的方式是

input[name="button_format"] 
0

注意:在jQuery 1.3 [@attr]樣式選擇器被刪除(它們是 以前在jQuery 1.2中不推薦使用)。只需從選擇器中刪除'@'符號 以使其再次工作。

這是一個不推薦使用的選擇器,您必須將其從代碼中移除,因爲它不再受支持,並且會導致錯誤。

0

這是做它的老樣子。

這是一樣的(和應改爲):

$('input[name="button_format"]') 

還要注意強制性報價

0

屬性爲他人提及。

我2美分:記號從XPath,它也表示(XML)的啓發屬性使用@