javascript Jquery .text() 不起作用

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

Jquery .text() not working

javascriptjqueryhtml

提问by shazinltc

I have an anchor tag like this:

我有一个这样的锚标签:

<a id="[email protected]" class="big-link" href="">4</a>

Now I am trying to replace the text with the following code

现在我试图用以下代码替换文本

a = 1;
b = 18;
c = "[email protected]";
var tempId = "#"+a+"_"+b+"_"+c;
$(tempId).text("some text");

Thiis never works. I tried .html(), .append(). Looks like it never finds the ID. But it works if I call the event by class name.

这永远不会奏效。我试过 .html(), .append()。看起来它永远找不到ID。但如果我按类名调用事件,它就可以工作。

$(".big-link").text("some text");

Any clue? Thanks in advance.

有什么线索吗?提前致谢。

回答by ryadavilli

The problem is with the way you are constructing your ID values. Issues are

问题在于您构建 ID 值的方式。问题是

  1. Starting with a digit
  2. Use of @
  3. Use of .
  1. 以数字开头
  2. 用于 @
  3. 用于 。

I removed those and jquery text() was working fine for me. Check this fiddle : http://jsfiddle.net/kDnmu/

我删除了那些,jquery text() 对我来说工作正常。检查这个小提琴:http: //jsfiddle.net/kDnmu/

HTML

HTML

<a id="A_1_18_shazin_mycompany-com" class="big-link" href="">4</a>

Jquery

查询

a = 1;
b = 18;
c = "shazin_mycompany-com";
var tempId = "#A_"+a+"_"+b+"_"+c;
$(tempId).text("some text");

For details on what is a valid id check this SO answer: What are valid values for the id attribute in HTML?

有关什么是有效 id 的详细信息,请查看此 SO 答案:HTML 中 id 属性的有效值是什么?

EDIT:If you are constructing these IDs dynamically, consider escaping them with some logic of your own.

编辑:如果您正在动态构建这些 ID,请考虑使用您自己的一些逻辑来转义它们。

回答by Jai

Try this: http://jsfiddle.net/LPuTr/

试试这个:http: //jsfiddle.net/LPuTr/

a = 1;
b = 18;
c = "[email protected]";
aaa = a+'_'+b+'_'+c;

$('a[id="'+aaa+'"]').text("some text");

for more info for idsin html 5see this: http://mathiasbynens.be/notes/html5-id-class

有关更多信息idshtml 5请参见:http: //mathiasbynens.be/notes/html5-id-class

回答by tusar

You need to escape the dot(.) and @characters with \\. try this

您需要使用 . 转义点(。)和@字符\\。试试这个

$("#1_18_shazin\@mycompany\.com").text("some text");

Try this : http://jsfiddle.net/HLtz9/10/

试试这个:http: //jsfiddle.net/HLtz9/10/

回答by petermk

You have illegal characters in your id, also an id cannot begin with a digit

您的 ID 中有非法字符,而且 ID 不能以数字开头

What are valid values for the id attribute in HTML?

HTML 中 id 属性的有效值是什么?

回答by elrado

It looks to me you have to loose period . and alpha @ and then it works!

在我看来,您必须要放松经期。和 alpha @ 然后它就起作用了!

What are valid values for the id attribute in HTML?

HTML 中 id 属性的有效值是什么?

I understand why alpha, but I have no clue for period :/

我明白为什么是 alpha,但我不知道期间:/

回答by Zaheer Ahmed

Here is working JSfiddle

这是工作JSfiddle

Problem is you are using @and .in your id

问题是您正在使用@.在您的 ID 中

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

ID 和 NAME 标记必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_") 、冒号 (":") 和句点 (".")。

@is not legal for ID while .and :is allowed but one should avoid it because:

@对 ID 来说是不合法的,.并且:是允许的,但应该避免它,因为:

For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c"

例如,一个 ID 可以被标记为“ab:c”并在样式表中被引用为 #ab:c 但作为元素的 id,它可能意味着 id“a”,类“b”,伪-选择器“c”

.

.