如何使用jquery更新标签标签
我有以下 HTML:
<div id="myDiv">
<table id="tbl0">
<tr id="tr0" style="display: none;">
<td>
<label id="lbl0"></label>
</td>
</tr>
<tr id="tr1" style="display: none;">
<td>
<label id="lbl1"></label>
</td>
</tr>
<tr id="tr2" style="display: none;">
<td>
<label id="lbl2"></label>
</td>
</tr>
</table>
</div>
和以下 jquery 将一行设置为可见并用一些文本更新(但失败)标签标记。
var myStr = $(this).text();
var myArr = myStr.split(',');
$.each(myArr, function (i) {
// This part works just fine
var tr = $('#myDiv').find("#tr" + i);
tr.css('display', 'inline');
// The label is found, but I can't get jquery to update the text of
// ...it no matter what I try
var lbl = tr.find("lbl" + i);
lbl.val('hello world'); // doesn't work
lbl.text('hello world'); // doesn't work
lbl.html('hello world'); // doesn't work
});
那么我在这里做错了什么?
I have the following HTML:
<div id="myDiv">
<table id="tbl0">
<tr id="tr0" style="display: none;">
<td>
<label id="lbl0"></label>
</td>
</tr>
<tr id="tr1" style="display: none;">
<td>
<label id="lbl1"></label>
</td>
</tr>
<tr id="tr2" style="display: none;">
<td>
<label id="lbl2"></label>
</td>
</tr>
</table>
</div>
And the following jquery that sets a row to visible and updates (but fails) the label tag with some text.
var myStr = $(this).text();
var myArr = myStr.split(',');
$.each(myArr, function (i) {
// This part works just fine
var tr = $('#myDiv').find("#tr" + i);
tr.css('display', 'inline');
// The label is found, but I can't get jquery to update the text of
// ...it no matter what I try
var lbl = tr.find("lbl" + i);
lbl.val('hello world'); // doesn't work
lbl.text('hello world'); // doesn't work
lbl.html('hello world'); // doesn't work
});
So what am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个...
您正在尝试选择一个
标记,该标记显然不存在,请使用
#
指定要搜索的 id。try this...
You are trying to select a
<lbl1/>
tag, which obviously doesn't existuse
#
to specify an id to search for.你对找到的标签是错误的,你需要使用
#
来指定它是一个id:两者:
都是设置标签文本的正确方法,我更喜欢
。 html()
因为它不解析 htmlspecialchars 中的字符串,所以可能会稍微快一些。You're wrong about the label being found, you need to use a
#
to specify that it is an id:Both:
are correct ways to set the text of the label, I prefer
.html()
since it doesn't parse the string from htmlspecialchars it might be slightly faster.错误在这一行:
您忘记了 # 符号,因为您正在按 ID 查找。
The error is in this line:
You forgot # sign, since you are looking up by ID.
我偶然发现了这个线程,虽然它帮助我走向正确的方向,但一旦我通过在标签元素上设置
innerHTML
属性找到我的标签,我就能够解决一些不同的问题。我正在使用 jQuery。I stumbled upon this thread and while it helped lead me in the right direction I was able to solve a little different way once I found my label by setting the
innerHTML
property on the label element. I was using jQuery.