Javascript 无法使用jquery克隆没有文本的段落
这个例子来自一本书。 它告诉我,如果我将克隆中的参数设置为false,则可以克隆没有文本的段落。 我检查了jQuery中的文档,并尝试了所有true和false的组合。 结果证明,没有文本我无法复制这段文字。 这只是jQuery中的一个bug吗Javascript 无法使用jquery克隆没有文本的段落,javascript,jquery,html,Javascript,Jquery,Html,这个例子来自一本书。 它告诉我,如果我将克隆中的参数设置为false,则可以克隆没有文本的段落。 我检查了jQuery中的文档,并尝试了所有true和false的组合。 结果证明,没有文本我无法复制这段文字。 这只是jQuery中的一个bug吗 <html> <head> <title>Copying element</title> <script src="../jquery-1.8.0.js" ty
<html>
<head>
<title>Copying element</title>
<script src="../jquery-1.8.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
//copy elment and insert
$('div.chapter p:eq(0)').clone(false).insertBefore('div.chapter');
});
</script>
<style type="text/css">
span.footnote{
font-style:italic;
display:block;
}
.chapter span.footnote{
display:inline;
}
.chapter p{
width:50%;
}
</style>
</head>
<body>
<h1 id="f-title">Flatland: A ROmance of Many Dimension</h1>
<div id="f-author">by Edwin A. Abbott</div>
<h2>Part 1, Section 3</h2>
<h3 id="f-subtitle">Concerning the Inhabitants of Flatland</h3>
<div id="excerpt">an excerpt</div>
<div class="chapter">
<p class="squre">Our professional Men and Gentlemen are Squares (to which class I myself belong) and FIve-Sided Figures
or Pentagons.
</p>
<p class="mobility hexagon">Next above these come the Nobility of whoem there are serverl degress
<a href="http://en.wikipedia.org/wiki/Hexagon">Hexagons</a>and from thence rising in the number of theirside till they received the honourable title of
<a href="http://en.wikipedia.org/wiki/Polygon">Polygonal</a>
</p>
<p><span class="pull-quote">It is a <span class="drop">Law of Nature</span>with us that a mable child shall have </span>
</p>
<p>But this rule applies not always to the Tradesman</p>
<p>Rarely -- in proportion to the vast numbers of Isosceles
</p>
<p>The birth of tha Ture Equlaterial Traignel from Isoscele parents is the subject of rejociing in the coutry for many furlongs.
</p>
<p>How admirable is the Law of Compensation!
</p>
</div>
</body>
</html>
重读这本书
它指定元素与其内容深度副本一起复制。布尔参数仅处理事件处理程序和,而不处理嵌入元素
所以这是正常的行为:div.chapter$'div.chapter p:eq0'中的第一段与内容一起复制
如果需要非深度克隆功能,可以使用以下功能:
// clone an element with attributes, without the weight of calling clone and emptying it
// (call it on a non jquery element, using [0] if necessary)
function nondeepclone(element) {
var newelem = document.createElement(element.tagName);
for (var i = 0; i < element.attributes.length; i++) {
var attr = element.attributes[i];
newelem.setAttribute(attr.name, attr.value);
}
return newelem;
}
但我不确定它是否有真正的用途。请重新阅读
它指定元素与其内容深度副本一起复制。布尔参数仅处理事件处理程序和,而不处理嵌入元素
所以这是正常的行为:div.chapter$'div.chapter p:eq0'中的第一段与内容一起复制
如果需要非深度克隆功能,可以使用以下功能:
// clone an element with attributes, without the weight of calling clone and emptying it
// (call it on a non jquery element, using [0] if necessary)
function nondeepclone(element) {
var newelem = document.createElement(element.tagName);
for (var i = 0; i < element.attributes.length; i++) {
var attr = element.attributes[i];
newelem.setAttribute(attr.name, attr.value);
}
return newelem;
}
但我不确定这是否有真正的用途。我认为如果没有文本,你无法克隆这段文字。但是,您可以在克隆后立即将其清除,方法是将其文本设置为空白。或者使用。按照jackwanders的建议清空 见:
请参阅:如果没有文本,我认为您无法克隆该段落。但是,您可以在克隆后立即将其清除,方法是将其文本设置为空白。或者使用。按照jackwanders的建议清空 见:
请看:我想您想要的是:
$('div.chapter p:eq(0)').clone(false).empty().insertBefore('div.chapter');
这将克隆段落,删除其子段落,然后插入。clonefalse仅表示附加到原始元素的数据和事件不会复制到克隆中。我想您需要的是:
$('div.chapter p:eq(0)').clone(false).empty().insertBefore('div.chapter');
这将克隆段落,删除其子段落,然后插入。clonefalse只意味着附加到原始元素的数据和事件不会复制到克隆中。我认为您需要的是-
$(document).ready(function(){
//copy element and insert
var clone = $('div.chapter p:eq(0)').clone().empty();
clone.insertBefore('div.chapter');
});
克隆后的empty方法删除所有子元素,即所选HTML元素的数据。我想您需要的是-
$(document).ready(function(){
//copy element and insert
var clone = $('div.chapter p:eq(0)').clone().empty();
clone.insertBefore('div.chapter');
});
克隆后的empty方法将删除所有子元素和所选HTML元素的数据。书籍告诉我,我的克隆结果可以是:,Jquery文档中有一个参数[withDataAndEventsA Boolean指示是否应将事件处理程序和数据与元素一起复制],所以我想我可以不用文字复制它。你误解了文档。true/false参数是:一个布尔值,指示是否应将事件处理程序与元素一起复制。数据不是元素的文本,而是附加到元素的数据:您是说要克隆一个段落,然后从克隆中删除内容吗?回答Rocket:您是对的:我没有看到句子[由.data方法附加的所有元素数据也复制到新副本。]书中告诉我,我的克隆结果可以是:,Jquery文档中有一个参数[withDataAndEventsA Boolean,指示是否应将事件处理程序和数据与元素一起复制],因此我想我可以在没有文本的情况下克隆它。您误解了文档。true/false参数是:一个布尔值,指示是否应将事件处理程序与元素一起复制。数据不是元素的文本,而是附加到元素的数据:您是说要克隆一个段落,然后从克隆中删除内容吗?回答Rocket:您是对的:我没有看到句子[由.data方法附加的所有元素数据也复制到新副本。]我看错了文档,你是对的:这本书也犯了一个错误,奥兹特的数据词可能模棱两可,对吧?这就是我添加第二个链接的原因。我错误地理解了文档,你是对的:这本书也犯了一个错误,orzIt的可能是数据词,不明确,对吗?这就是我添加第二个链接的原因。