我希望能够在 input
元素中设置 value
属性的内容,但尚未成功执行此操作。
HTML:
<div class='photo-info'>
Photo Name : <span class='photo-name'><?php echo $title; ?></span>
<span class='title-edit'><a href='#'>Edit</a></span>
</div>
jQuery:
// show editable area for title
$('.title-edit').on('click', function () {
var $titleElm = $(this).prev();
var text = $titleElm.text();
$titleElm.html("<input type='text'>").val('tom');
})
结果:
最佳答案
更新 HTML 后添加 find('input')
:
$titleElm.html("<input type='text'>").find('input').val('tom');
关于javascript - jQuery 使用 .html() 设置输入值属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163614/