简体   繁体   English

.replaceWith()在jQuery 1.9+中不起作用

[英].replaceWith() is not working in jQuery 1.9+

I'm trying to clone a <textarea> and clone and replace the digit in the label <label> Number 1 <label> increasing by 1 each time the add button is pressed (So the first label will have Number 1, the label underneath Number 2 etc). 我正在尝试克隆<textarea>并克隆并替换标签<label> Number 1 <label>的数字,每次按下添加按钮时,该数字增加1(因此第一个标签将具有Number 1,其下方的标签2等)。

This works with jQuery 1.8 and below but anything above does not clone and add 1 to the digit. 这适用于jQuery 1.8及以下版本,但以上任何内容均不会克隆并将数字加1。

HTML HTML

<div>
 <label for="number">Number <span class="one">1</span></label>
 <textarea id="number"></textarea>
</div>
<button>Add</button>

jQuery jQuery的

var $row = $('div').clone(),
    cloneCount = 2;

$('button').click(function () {
    $row.clone().insertBefore($("button"));
    $('span').clone().attr('span', cloneCount++).replaceWith($('[class=one]:last')).text(cloneCount - 1);
});

JSFIDDLE: http://jsfiddle.net/wba6jvkj/ JSFIDDLE: http//jsfiddle.net/wba6jvkj/

I don't know what you were attempting with .attr('span' and why it seemed to work in < 1.8, or why you are subtracting one from cloneCount , but this should do what you want: 我不知道您在尝试使用.attr('span'为何,为什么它似乎在<1.8中有效,或者您为什么要从cloneCount中减去一个,但这不应该做您想要的:

var $row = $('div').clone(),
    cloneCount = 2;

$('button').click(function () {
    $row.clone().insertBefore($("button"));
    $('span.one:last').text(cloneCount++);
});

jsFiddle example jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM