将类的内容添加到 id 中?

Posted

技术标签:

【中文标题】将类的内容添加到 id 中?【英文标题】:Prepend the content of a class into an id? 【发布时间】:2013-07-03 00:50:10 【问题描述】:

我正在尝试将类的内容移动(特别添加)到 div 中。

这是我要移动的班级:

<font class="pricecolor colors_productprice">
   <span class="PageText_L483n">
      <span itemprop='price'>$11.00</span> per 2 oz bag</span>
</font>

这是我希望将其移入的 id:

<td valign="top">
   <span class="PageText_L71n">Quantity:</span>
   <input type="text" class="v65-productdetail-cartqty" name="QTY.KCJ-CP" size="3">
</td>

这是当前不成功的 jQuery 尝试:

<script>
$(document).ready(function()
    $("#v65-productdetail-action-wrapper").prepend(".PageText_L483n");
);
</script>

您无法从提供的代码中看出,但“#v65-productdetail-action-wrapper”是第二个代码块所在的容器。您可能已经知道,这只是在文字字符串前面加上“。 PageText_L483n",而不是它的内容。

【问题讨论】:

【参考方案1】:
$("#v65-productdetail-action-wrapper").prepend($(".PageText_L483n").first());

删除

$(".PageText_L483n").first().html("");

或者

$(".PageText_L483n").first().remove();

两者都应该工作。

你需要做的就是将一个 添加到类中,并将它添加到 id 中。

$("#v65-productdetail-action-wrapper").prepend($(".PageText_L483n").first().append("<br>"));

应该这样做。

【讨论】:

这种工作,虽然有两个问题:1)它移动了文本,但它根本没有样式。我怎样才能保持风格? 2)它没有摆脱原始文本,所以现在它显示了两次。我怎样才能让它也隐藏原文? 从给定的代码中删除.html(),它应该可以工作。您不能轻易地“复制”样式 - 您最好将 CSS 设置为两个位置的元素样式。 这太接近了,虽然还有一个问题,就是它没有出现在 id 内容的正上方,而是出现在它的旁边,这将其他内容推到了上面。这是它现在的样子:i.imgur.com/YOJPeSQ.png。除了附加它之外,还有什么方法可以添加中断标签或其他东西吗? 如果你希望它高于你指定的 id,你需要一个 $("#v65-productdetail-action-wrapper").before($(".PageText_L483n").first()); 试试$("#v65-productdetail-action-wrapper").prepend($(".PageText_L483n").first(), "&lt;br&gt;");$("#v65-productdetail-action-wrapper").prepend($(".PageText_L483n").first(), "&lt;div style='clear:both;'&gt;&lt;/div&gt;");

以上是关于将类的内容添加到 id 中?的主要内容,如果未能解决你的问题,请参考以下文章

仅当子容器具有内容时,如何将类添加到特定的父元素

如何使用装饰器将类的方法添加到类内的列表中

如何将类、id、占位符属性添加到 django 模型表单中的字段

Drupal 将类添加到特定于节点类型的行

如果链接包含特定文本,则jQuery将类添加到href

如何将类添加到 Django 表单中的字段标记