简体   繁体   English

将文本添加到多个div标签中的跨度

[英]Add text to a span within multiple div tags

I have a html content in which a span tag is placed inside multiple div tags. 我有一个html内容,其中span标签位于多个div标签内。

For example : 例如 :

<div id="ae_launcher" class="module tipsytip active" tabindex="0" role="button" aria-label="" style="display: none;" original-title="">
    <div class="left">
        <span class="copy-narrow" aria-hidden="true"></span>
    </div>
</div>

Also, please note the class "ae-left" has been used 3 times in the entire page. 另外,请注意,“ ae-left”类已在整个页面中使用了3次。

I need to add text inside the span tag. 我需要在span标签内添加文本。

I tried something like this: 我尝试过这样的事情:

<script type="text/javascript">
    $('.left:eq(1) .copy-narrow').text('hidshjgdjb');
</script>

But this din't solve the issue. 但这并不能解决问题。

你可以通过jQuery做到这一点

$('#ae_launcher .ae-left > span ').text("Hello");

假设您在所有3个地方都使用ae-left div类作为跨度的父级

$('.ae-left span ').text("Text here");
<script>
    var l = document.getElementsByClassName("ae-copy-narrow");
                l[x].innerHTML = "some text";

</script>

x is the location you want to place between 0-2. x是您要放置在0-2之间的位置。

You can do it in this way: 您可以通过以下方式进行操作:

$('.left > .copy-narrow').text("your Text");

This will set the innerText property of all elements with .copy-narrow class to your Text . 这会将所有带有.copy-narrow类的元素的innerText属性设置为your Text
If you want to select this particular span only, you can always use the id attribute of the parent div as id attribute must be unique. 如果只想选择此特定范围,则始终可以使用父div的id属性,因为id属性必须唯一。

$('#ae_launcher .copy-narrow').text('your text');

You can call the class and use the .text() : 您可以调用class并使用.text()

<script>
    $(function() {
        var sometext = 'This is my text';
        $('.ae-copy-narrow').text(sometext);
    });
</script>

Or if you want to have html tags you can use .html() : 或者,如果您想要html标记,则可以使用.html()

<script>
    $(function() {
        var sometext = '<img src="myimage.png" />';
        $('.ae-copy-narrow').html(sometext);
    });
</script>

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

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