$(htmlString).attr("value", "newValue");
但这将返回 jQuery 对象,而不是字符串。您可以将其添加到 DOM。
$(htmlString).attr("value", "newValue").appendTo("body"); // you can give any element instead of body
编辑:
您可以使用@idor_brad 的方法。这是最好的方法,否则
var htmlString = '<input type="text" id="someID" name="someID">';
var $htmlString = $(htmlString);
$htmlString.attr("value1", "newValue1");
$htmlString.attr("value2", "newValue2");
$htmlString.attr("value3", "newValue3");
console.log($htmlString.get(0).outerHTML);
或
var htmlString = '<input type="text" id="someID" name="someID">';
var $htmlString = $(htmlString);
$htmlString.attr("value1", "newValue1");
$htmlString.attr("value2", "newValue2");
$htmlString.attr("value3", "newValue3");
console.log($("<div>").append($htmlString).html());