2012-04-12 3 views
0

ドキュメントに2つのpタグがあるとします。 jQueryonMouseOverイベントが発生した場合、2つの異なるエフェクトを呼び出す必要があります。これらの2つのタグにIdを付ける必要がありますか?これらのタグにIDを渡すことなく達成できますか?jquery関数の適用にIdが必要ですか?

+5

は、返信用http://api.jquery.com/category/selectors/ – simon

答えて

5

にはがありますが、何かにはidが含まれていますが、要素を一意に識別するための最良の方法です。

あなたはクラスによって代わりidenfityことができます:セレクタの完全なリストはdocumentationで利用可能です

$("p:eq(2)") 

:親での位置によって

$("[src='image.jpg']") 

:属性によって

$(".myClass") 

+0

感謝を参照してください!!どちらの 'p'タグも同じクラスを持っています。段落でクリックイベントが発生すると、さまざまな機能を適用したいと考えています。だから私は両方の段落にIDを付ける必要があると思いますか? – niting112

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

関連する問題