简体   繁体   English

jQuery .append()仅在第一个元素上

[英]jQuery .append() only on first element

<div class="append-something first"></div>
<div class="append-something second"></div>
<div class="append-something third"></div>

$("#trigger").click(function() {
 $(".append-something").append("Some Stuff");
});

Hi there, i want the above code to paste/append the words "Some Stuff" into the div-class "append-something" but only into the first div with the class-name. 嗨,我要上面的代码将单词“ Some Stuff”粘贴/附加到div类的“ append-something”中,但仅粘贴到具有类名的第一个div中。 (In my code the words should append to "first", not to "second" or "third"). (在我的代码中,单词应附加到“第一”之后,而不是“第二”或“第三”之后)。 Thanks a lot! 非常感谢!

You can use the :first or the :first-child selector. 您可以使用:first:first-child选择器。 For only the last you should use the :last selector. 对于最后一个,您应该使用:last选择器。

For any other element you can use the index of the element. 对于任何其他元素,您可以使用元素的索引。 jQuery will create an array of all the elements he finds with append-something class. jQuery将使用append-something类创建一个由他找到的所有元素组成的数组。 So the first element will be index of 0 . 因此,第一个元素将是索引0

$("#trigger-first").click(function() {
// ":first" and ":first-child" will work.
    $(".append-something:first").append("Some Stuff");
});

$("#trigger-last").click(function() {
// ":last" for last option.
    $(".append-something:last").append("Some Stuff");
});

$("#trigger-second").click(function() {
// It will return an array of item so you can request on index.
    $($(".append-something")[1]).append("Some Stuff");
});

To play with it: https://jsfiddle.net/9bfskesb/ 要使用它: https : //jsfiddle.net/9bfskesb/

您可以使用第一个选择器

 $(".append-something:first").append("Some Stuff");

Use $(".append-something:first").append("Some Stuff"); 使用$(".append-something:first").append("Some Stuff"); or if you have the class .first then simply $(".append-something.first").append("Some Stuff"); 或者,如果您拥有.first类,则只需$(".append-something.first").append("Some Stuff");

 $("#trigger").click(function() { $(".append-something.first").append("Some Stuff"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="append-something first"></div> <div class="append-something second"></div> <div class="append-something third"></div> <button id="trigger">click</button> 

 $("#trigger").click(function() { $(".append-something:first").append("Some Stuff"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="append-something first"></div> <div class="append-something second"></div> <div class="append-something third"></div> <button id="trigger">click</button> 

You Can use prependTo() method 

 $("<span>Hello World!</span>").prependTo(".inner:first-child"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="inner">Hello</div> <div class="inner">Goodbye</div> </div> 

You can use :first selector for this 您可以使用:first选择器

 $("#trigger").click(function() { $(".append-something:first").append("Some Stuff"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="append-something"></div> <div class="append-something"></div> <div class="append-something"></div> <button id="trigger">click</button> 

you have already three different classes for each div ie first , second , third . 每个div已经有三个不同的类,即firstsecondthird You can use the classes specifically to append the html as 您可以使用专门的类将html附加为

 $(".first").append("Some Stuff");

But if you want to add using the same class for each div the you should go with 但是,如果要为每个div使用相同的类,则应该使用

 $(".append-something:first").append("Some Stuff");
$(".append-something").first().append("Some Stuff");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM