jQuery:将 attr 值占位符替换为来自文本输入的用户输入

jQuery: Replace attr value placeholder with user input from text input

使用 .replaceWith().val() 我可以替换标题,但是在尝试访问输入的 placeholder .attr() 时,我无法替换它.这是我拥有的:

<p class="title">Title</p>
<p><input class="placeholder" type="text" placeholder="Placeholder" disabled /></p>
<p><input class="newText" type="text" /></p>
<p><input class="newPlaceholder" type="text" /></p>
<p><input type="button" class="button" title="button" /></p>

$("input.button").click(function(){
    var newText = $("input.newText").val();
    $("p.title").replaceWith(newText);

    var newPlaceholder = $("input.newPlaceholder").val();
    $("input.placeholder").attr("placeholder").replaceWith(newPlaceholder);
});

JSFiddle

.replaceWith() isn't used to set properties of HTML elements. You want to use .prop():

Get the value of a property for the first element in the set of matched elements or set one or more properties for every matched element

要设置文本框的占位符 属性,您可以像这样使用 .prop()

var newPlaceholder = $("input.newPlaceholder").val();
$("input.placeholder").prop("placeholder", newPlaceholder);

我已经 updated your JSFiddle 举了一个例子。