0

次のコードの実際の違いは何ですか:

<p>Maizere1</p>
<p>Maizere2</p>
<p>Maizere3</p>
<script>
$("p").text($(this).text()).get(0);

vs

$("<p>").text($(this).text()).get(0);//actually this line is what giving me trouble 

what does $("<p>"> this do?
i heard that $("<p>") will first actually check where the element exist or not ,if not only will create element 
4

3 に答える 3

5

$('<p>')有効なjqueryセレクター<p>ではありませんが、ここで解決しようとしているとは思わない要素を作成します。

于 2013-02-05T16:31:50.837 に答える
2

$("p")-p要素を選択します
$("<p>")-その場でp要素を作成します

于 2013-02-05T16:34:20.230 に答える
2

$("<p>")新しい要素を作成し、それらを選択している.append()間にそれを dom に追加するために使用できます。$("p")

正しいのは、$("<p>")次のように使用することです: $("<p />"). しかし、jQuery は両方を許可します。

例:

<p></p>

$("p").append($("<p>test</p>").addCLass("test"));

結果:

<p class="test">
  <p>test</p>
</p>
于 2013-02-05T16:34:45.570 に答える