0

ドキュメントに2つのpタグがあるとします。onMouseOverイベントが発生したときに、jQueryを使用して2つの異なるエフェクトを呼び出したいと思います。これらの2つのタグにIDを指定する必要がありますか。これらのタグにIDを付与せずに達成することはできませんか?p

4

4 に答える 4

5

何も指定する必要はありませんid、要素を一意に識別するための最良の方法です。

代わりに、クラスごとに識別できます。

$(".myClass")

属性別:

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

親の位置別:

$("p:eq(2)")

セレクターの完全なリストは、ドキュメントで入手できます。

于 2012-04-12T11:06:32.923 に答える
5
$('p:first'); // first p tag
$('p:last'); // last p tag
$('p').eq(1); // exactly the second p tag
于 2012-04-12T11:08:45.620 に答える
3

1つまたは複数の要素を選択する方法はいくつかあります。

$('.classname')

$('#id')

$('tagname')

$('[attr="value"]')

于 2012-04-12T11:08:07.743 に答える
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 –要素のチートシートを選択

于 2012-04-12T11:15:17.693 に答える