如何使用jquery更新标签标签

发布于 2024-11-30 23:43:29 字数 1107 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守望孤独 2024-12-07 23:43:29

试试这个...

var lbl = tr.find("#lbl" + i);

您正在尝试选择一个 标记,该标记显然不存在,

请使用 # 指定要搜索的 id。

try this...

var lbl = tr.find("#lbl" + i);

You are trying to select a <lbl1/> tag, which obviously doesn't exist

use # to specify an id to search for.

沐歌 2024-12-07 23:43:29

你对找到的标签是错误的,你需要使用#来指定它是一个id:

var lbl = tr.find("#lbl" + i);

两者:

lbl.text('hello world');
lbl.html('hello world');

都是设置标签文本的正确方法,我更喜欢。 html() 因为它不解析 htmlspecialchars 中的字符串,所以可能会稍微快一些。

You're wrong about the label being found, you need to use a # to specify that it is an id:

var lbl = tr.find("#lbl" + i);

Both:

lbl.text('hello world');
lbl.html('hello world');

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.

向地狱狂奔 2024-12-07 23:43:29

错误在这一行:

var lbl = tr.find("#lbl" + i);

您忘记了 # 符号,因为您正在按 ID 查找。

The error is in this line:

var lbl = tr.find("#lbl" + i);

You forgot # sign, since you are looking up by ID.

没企图 2024-12-07 23:43:29

我偶然发现了这个线程,虽然它帮助我走向正确的方向,但一旦我通过在标签元素上设置 innerHTML 属性找到我的标签,我就能够解决一些不同的问题。我正在使用 jQuery。

var labels = document.getElementsByClassName('someclassname');

$(labels).each(function (index, element) {    
    element.innerHTML = 'hello world';
});

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.

var labels = document.getElementsByClassName('someclassname');

$(labels).each(function (index, element) {    
    element.innerHTML = 'hello world';
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文