jQuery 在跨度中设置文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13492967/
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-26 12:40:26  来源:igfitidea点击:

Set the text in a span

jquery

提问by Ayman Hussein

I have this span

我有这个跨度

<a title="Prev" data-event="click" 
   data-handler="prev" class="ui-datepicker-prev   ui-corner-all">
     <span class="ui-icon ui-icon-circle-triangle-w">Prev</span>
</a>

I need to set the text of the span to be <<instead of its current text Prev.

我需要将 span 的 text 设置为<<而不是它的当前 text Prev

I tried this below, but it didn't change the text as I'd expected. How can this be done?

我在下面尝试了这个,但它没有像我预期的那样改变文本。如何才能做到这一点?

 $(".ui-icon .ui-icon-circle-triangle-w").html('<<');

回答by Curt

Use .text()instead, and change your selector:

改用.text()并更改您的选择器:

$(".ui-datepicker-prev .ui-icon.ui-icon-circle-triangle-w").text('<<');

-- VIEW DEMO --

-- 查看演示 --

回答by VisioN

This is because you have wrong selector. According to your markup, .ui-iconand .ui-icon-circle-triangle-w"should point to the same <span>element. So you should use:

这是因为您选择了错误的选择器。根据您的标记,.ui-icon.ui-icon-circle-triangle-w"应指向相同的<span>元素。所以你应该使用:

$(".ui-icon.ui-icon-circle-triangle-w").html("<<");

or

或者

$(".ui-datepicker-prev .ui-icon").html("<<");

or

或者

$(".ui-datepicker-prev span").html("<<");

回答by Alnitak

You need to fix your selector. Although CSS syntax requires multiple classes to be space separated, selector syntax would require them to be directly concatenated, and dot prefixed:

您需要修复您的选择器。虽然 CSS 语法要求多个类用空格分隔,但选择器语法要求它们直接连接,并以点为前缀:

$(".ui-icon.ui-icon-circle-triangle-w").text(...);

or better:

或更好:

$(".ui-datepicker-prev > span").text(...);

回答by Anton

$('.ui-icon-circle-triangle-w').text('<<');

回答by freewill

Give an ID to your span and then change the text of target span.

为您的跨度指定一个 ID,然后更改目标跨度的文本。

$("#StatusTitle").text("Info");
$("#StatusTitleIcon").removeClass("fa-exclamation").addClass("fa-info-circle"); 

<i id="StatusTitleIcon" class="fa fa-exclamation fa-fw"></i>
<span id="StatusTitle">Error</span>

Here "Error" text will become "Info" and their fontawesome icons will be changed as well.

这里“错误”文本将变成“信息”,它们的字体图标也将更改。

回答by Aamir

Try it.. It will first look for anchor tag that contain span with class "ui-icon-circle-triangle-w", then it set the text of span to "<<".

试试吧..它会首先寻找包含类为“ui-icon-circle-triangle-w”的span的锚标签,然后将span的文本设置为“<<”。

$('a span.ui-icon-circle-triangle-w').text('<<');