24

私はこれを以下で試しました。$( "div.tab_select")[0]の戻りオブジェクトはjQueryオブジェクトではないと思いますが、純粋なjavascriptメソッドを使用することさえできません。

jQueryオブジェクトにする方法はありますか?たとえば、$($( "div.tab_select")[0]) ..これはばかげていることを知っています。

読んでくれてありがとう。

var tmp = $("div.tab_select")[0]; 
alert(tmp); //This gives me HTMLDivElement collectly. But I can't use any of javascript..

alert(tmp.nodeName); //But this give me error "Uncaught TypeError: Cannot read property 'nodeName' of undefined"

tmp.hide(); //Neither, I can't use this.
4

4 に答える 4

39
// all divs with tab_select class
$('div.tab_select')

// first div with tab_select class
$('div.tab_select:first')

// or CSS pseudo selector which is slightly faster than the first jQuery 
// shortcut 
$('div.tab_select:first-of-type')

// or
$('div.tab_select').first()

// or
$('div.tab_select:eq(0)')

// or
$('div.tab_select').eq(0)
于 2012-05-11T19:44:56.410 に答える
1

jQueryオブジェクトが必要な場合は、var tmp = $("div.tab_select:first")代わりに使用してください。

var tmp = $("div.tab_select")[0]DOM要素を返します(存在する場合)

于 2012-05-11T19:45:29.937 に答える
1

ただやってください$(tmp)。[0]は、JQueryインスタンスではなくHTML要素を提供します。

于 2012-05-11T19:45:54.303 に答える
0

私はどこかでcssセレクターの方が速いと読んだ。

$('div.tab_select:nth-child(n)').<method>

ここに例があります

于 2012-05-11T22:11:31.083 に答える