問題描述
我需要重新提出我的老問題,我可能不應該在凌晨 1 點問過它:P
I need to reask my old question, I probably shouldnt have asked it at 1am :P
使用 jquery 的屬性選擇器似乎找不到某些屬性:
It seems that some attributes are not being found using jquery's attribute selector:
$("*[some=value]");
到目前為止,我似乎無法使用表單的 action 屬性和 img 的 src 屬性.是否有某個屬性列表不起作用,以便我可以為它們編寫自定義選擇器?
So far it seems that i cant use form's action attribute, and img's src attribute. Is there a list somewhere of attributes that do not work so i can write custom selectors for them?
再次感謝!
似乎沒有人相信某些選擇器不能按預期工作.看這個例子:在 this site (上面有 jquery 1.3 for firebugging)有一個看起來像這個:
No one seems to believe that some selectors do not work as expected. Look at this example: On this site (which has jquery 1.3 on it for firebugging) there is a form that looks like this:
<form style="display: inline;" method="get" action="list">
(它圍繞搜索當前下載"下拉菜單).如果您打開 firebug 并嘗試使用此選擇器:
(its around the 'search current downloads' dropdown). If you open firebug and try this selector:
$("form[action=list]");
您將無法選擇表單.action 屬性沒有什么特別之處.該頁面上徽標圖像的 src 也是如此:
you will NOT be able to select the form. There is nothing special about the action attribute. Same goes for the src of the logo image on that page:
<img alt="Logo" src="/p/aost/logo?logo_id=1238551994"/>
不起作用的選擇器是:
$("img[src=/p/aost/logo?logo_id=1238551994");
當然,我可以進行通配符匹配,這不是我想要的.
Sure, i can do wildcard matches, that is not what i am after.
推薦答案
沒有不受支持的屬性的列表",因為不應該有;這是 jQuery 中的一個錯誤.
There is no "list" of unsupported attributes because there shouldn't be; this is a bug in jQuery.
這里是公開的門票:
- 無法使用action屬性選擇表單
- attr "action" 的表單和 Selectors 的屬性過濾器
- 屬性src"的選擇器與 1.2.6 中的工作方式不同
顯然,這些錯誤之間的共同點是 jQuery 將您指定的選擇器字符串與完整的 URL 進行比較,而不是 HTML 中定義的實際 action/src 屬性.這解釋了為什么 attributeEndsWith 或 attributeContains 選擇器在這種情況下確實有效.
Apparently the common denominator between the bugs is that jQuery is comparing the selector string you specify against the full URL as opposed to the actual action/src attribute as it is defined in the HTML. This explains why the attributeEndsWith or the attributeContains selectors do work in this case.
我建議只為表單/圖像提供一個類/ID 并完成它.
I would recommend just giving the form/image a class/ID and getting it over with.
這篇關于jquery 屬性選擇器找不到屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!