Javascript 我如何使用 jQuery 将文本添加到特定的 div 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14278630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:15:06  来源:igfitidea点击:

How i do add text to specific div element using jQuery?

javascriptjqueryhtml

提问by user1311776

I am having a problem with jquery.My HTML is

我的 jquery 有问题。我的 HTML 是

<div id="first">
   <span class="test"></span>
</div>
<div id="second">
   <span class="test"></span>
<div> 

Now I am trying to add text in span using Jquery

现在我正在尝试使用 Jquery 在跨度中添加文本

$j('span.test').text('Welcome');

After that it becomes,

之后就变成了,

<div id="first">
    <span class="test">Welcome</span>
</div>
<div id="second">
    <span class="test">Welcome</span>
<div> 

But, I am tryting to achieve is,

但是,我试图实现的是,

<div id="first">
    <span class="test">Welcome</span>
</div>
<div id="second">
    <span class="test"></span>
<div>

How can i do that in jquery?

我如何在 jquery 中做到这一点?

Thanks

谢谢

采纳答案by Robert Koritnik

Several possible alternatives

几种可能的选择

Others have provided their answers already but let me give you some more alternatives.

其他人已经提供了他们的答案,但让我给你一些更多的选择。

$("span.test:first").text("Welcome");
$("#first span.test").text("Welcome");
$("span.test").first().text("Welcome");
$("span.test").eq(0).text("Welcome");
$("span.test", "#first").text("Welcome");

The second and last one likely being the fastest because they target particular container by ID.
(internal jQuery optimisations may prove me wrong with any future version)

第二个和最后一个可能是最快的,因为它们通过 ID 定位特定容器。
(内部 jQuery 优化可能会证明我对任何未来版本都是错误的)

Performance comparison

性能对比

Here's a JSPerf testthat performance compares upper possibilities. As anticipated, the second and last approaches are the fastest of all because element selection is much simplified. I've tried running them on Chrome and you may run them in other browsers if you want to and see differences.

这是一个JSPerf 测试,性能比较了较高的可能性。正如预期的那样,第二个和最后一个方法是最快的,因为元素选择被大大简化了。我试过在 Chrome 上运行它们,如果你想看到差异,你可以在其他浏览器中运行它们。

回答by Milimetric

$('#first span.test').text('Welcome');

jQuery selectors are CSS selectors extended with a bit of magic. You can find everything you need to know here: http://api.jquery.com/category/selectors/

jQuery 选择器是用一些魔法扩展的 CSS 选择器。你可以在这里找到你需要知道的一切:http: //api.jquery.com/category/selectors/

回答by Rory McCrossan

You need to use the :firstselector to say that you only want to add text to the first element the selector finds. Try this:

您需要使用:first选择器来说明您只想向选择器找到的第一个元素添加文本。尝试这个:

$j('span.test:first').text('Welcome');

Example fiddle

示例小提琴

Alternatively, you can use the idof the parent div to make the selector unique:

或者,您可以使用id父 div 的 使选择器唯一:

$j('#first .test').text('Welcome');

回答by David H

In this example you can do what RoRyMcCrossan For general selection i recommend you to look at this :http://api.jquery.com/child-selector/

在这个例子中,你可以做 RoRyMcCrossan 对于一般选择我建议你看看这个:http://api.jquery.com/child-selector/

Or you can use this

或者你可以使用这个

http://api.jquery.com/eq-selector/

http://api.jquery.com/eq-selector/

回答by rongsir

Since you have an id, which usually is unique on one page, it woud be best to use the following:

由于您有一个 ID,它通常在一页上是唯一的,因此最好使用以下内容:

$("#first span.test").text("Welcome");