[英]Add text to a span within multiple div tags
我有一个html内容,其中span标签位于多个div标签内。
例如 :
<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>
另外,请注意,“ ae-left”类已在整个页面中使用了3次。
我需要在span标签内添加文本。
我尝试过这样的事情:
<script type="text/javascript">
$('.left:eq(1) .copy-narrow').text('hidshjgdjb');
</script>
但这并不能解决问题。
你可以通过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是您要放置在0-2之间的位置。
您可以通过以下方式进行操作:
$('.left > .copy-narrow').text("your Text");
这会将所有带有.copy-narrow
类的元素的innerText属性设置为your Text
。
如果只想选择此特定范围,则始终可以使用父div的id属性,因为id属性必须唯一。
$('#ae_launcher .copy-narrow').text('your text');
您可以调用class
并使用.text()
:
<script>
$(function() {
var sometext = 'This is my text';
$('.ae-copy-narrow').text(sometext);
});
</script>
或者,如果您想要html标记,则可以使用.html()
:
<script>
$(function() {
var sometext = '<img src="myimage.png" />';
$('.ae-copy-narrow').html(sometext);
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.