假設我在文檔中有兩個p
標籤。我想用jQuery調用兩種不同的效果,當onMouseOver事件發生時。是否有必要給這兩個p
標籤給出ID。如果不給ID給這些標籤就不能實現它?在應用jquery函數時,Id是必需的嗎?
0
A
回答
5
你不有給任何一個id
,但它是唯一標識元素的最佳途徑。
可以代替idenfity按類:
$(".myClass")
按屬性:
$("[src='image.jpg']")
通過位置父:
$("p:eq(2)")
選擇的完整列表是在documentation可用
+0
感謝回答!兩個'p'標籤都具有相同的類別。我想在段落中發生點擊事件時應用不同的功能。所以對於這個我想我們需要給這兩個段落ID? – niting112 2012-04-12 11:08:23
3
有幾種方法來選擇一個元素/元素:
$('.classname')
$('#id')
$('tagname')
$('[attr="value"]')
等
5
$('p:first'); // first p tag
$('p:last'); // last p tag
$('p').eq(1); // exactly the second p tag
3
雖然jQuery的允許你寫的更快,更簡單的腳本,但不幸的是它讓你永遠不會了解真正的JavaScript。
$("*") //selects all elements.
$(":animated") //selects all elements that are currently animated.
$(":button") //selects all button elements and input elements with type="button".
$(":odd") //selects even elements.
$(":odd") //selects odd elements.$("p") selects all <p> elements.
$("p.intro") //selects all <p> elements with class="intro".
$("p#intro") //selects the first <p> elements with id="intro".
$(this) //Current HTML element
$("p#intro:first") //The first <p> element with id="intro"
$("p:eq(2)") // The third <p> element in the DOM
$(".intro") //All elements with class="intro"
$("#intro") //The first element with id="intro"
$("ul li:first") //The first <li> element of the first <ul>
$("ul li:first-child") //The first <li> element of every <ul>
$("[href]") //All elements with an href attribute
$("[href$='.jpg']") //All elements with an href attribute that ends with ".jpg"
$("[href='#']") //All elements with an href value equal to "#"
$("[href!='#']") //All elements with an href value NOT equal to "#"
$("div#intro .head") //All elements with class="head" inside a <div> element with id="intro"
的jQuery - 選擇元素cheat sheet
相關問題
- 1. 在jQuery中,零長度計時器仍然是必需的嗎?
- 2. mongoose-schema - 都是必需的子數組+父文檔ID嗎?
- 3. apl是ssl必需的嗎?
- 4. XAML - 是LayoutRoot必需的嗎?
- 5. GLES2是glBindAttribLocation()必需的嗎?
- 6. 「自我」是必需的嗎?
- 7. MongoDB - 是DBREF必需的嗎?
- 8. 對於mov構造函數,noexcept總是必需的嗎?
- 9. YII2錯誤 - 應用程序的「id」配置是必需的
- 10. 對象的引用是必需的嗎?
- 11. jQuery ajax()URL是必需的?
- 12. 安排在XML中是必需的嗎?
- 13. 現在angularjs elvis不是必需的嗎?
- 14. Gtk.Widget.destroy()在Python中是必需的嗎?
- 15. Smarty函數register_modifier()是否必需?
- 16. jQuery $ .ajax成功必須是回調函數嗎?
- 17. RemoteControlClient - 音頻聚焦是必需/必需的嗎?
- 18. MVVM中的ObjectDataProvider不是必需的嗎?
- 19. 在javascript中聲明函數時,我必須使用`var`嗎?
- 20. Java Web服務 - 軸是必需的嗎?
- 21. ASP.NET MVC Ajax是必需的Microsoft.ajax.js嗎?
- 22. android - call_phone權限是必需的嗎?
- 23. Java是創建AMI所必需的嗎?
- 24. PostgreSQL函數是原子嗎? (ID生成)
- 25. 必應地圖v8 - heatMap.setLocation不是函數
- 26. 供應商分支中是當前/必需的嗎?
- 27. 如果文件是必需的,Perl 6應該運行MAIN嗎?
- 28. jRails是Rails + jQuery必須的嗎?
- 29. C++ Unicode加密庫必需(是嗎?)
- 30. MVC ModelState錯誤「ID字段是必需的」,儘管數據庫生成ID
見http://api.jquery.com/category/selectors/ – simon 2012-04-12 11:06:01