【问题标题】:AppendTo Not Working On WebpageAppendTo 在网页上不起作用
【发布时间】:2013-01-20 23:00:41
【问题描述】:

我有这个 jQuery 脚本:

$('.company').each(function(i, item) {
  var tempTitle = item.title;
  $('Working').appendTo("div[title='" + tempTitle + "']");
});

这是 HTML:

<li><div class="company" title="32"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="33"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="34"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="35"></div><div class="shortdescription">Short Description Here</div></li>
<li><div class="company" title="36"></div><div class="shortdescription">Short Description Here</div></li>

并且没有在 div 中添加任何细节。

我的错误在哪里? O_o

【问题讨论】:

  • 你的选择器错误,$('Working') 选择了&lt;Working&gt;。但是,您试图在没有意义的每次迭代中移动相同的元素,并且您的标记无效。
  • 我认为 OP 正在尝试插入一个简单的字符串 'Working' 进行测试

标签: jquery appendto


【解决方案1】:

appendTo 需要附加 HTML 或 jQuery 包装元素。您正在传递简单的字符串 working

试试这个:

$('.company').each(function(i, item) {
  var tempTitle = item.title;
  $('<div>appeded by jQuery</div>').appendTo("div[title='" + tempTitle + "']");
});

现在将 &lt;div&gt;appeded by jQuery&lt;/div&gt; 替换为您要附加的 HTML。

【讨论】:

  • 反过来应该支持文本节点:$("div[title='" + tempTitle + "']").append('Working')
【解决方案2】:

在保持简单的基础上 - OP 的代码看起来可能是机器生成的,因此他可以依赖保持不变的结构

所以:

$('.company').each(function(i, item) {
  $(this).next().append('Working');
});

如果是这样的话会起作用。

$(this).next('.shortdescription').append(' Working');

为了安全

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多