我在“body”标签中有这些 HTML 代码:
<body>
<center>
Hello!<br />
<textarea cols="65" rows="10" id="code"></textarea>
</center>
</body>
我想将所有页面代码放在上面的文本区域中,并使用 jQuery 中的以下代码:
$("textarea#code").val( $('body').html() );
好的,现在我想让用户使用以下命令更改“textarea”中“body”的 HTML:
$("textarea#code").change(function(){
$('body').html( $(this).val() );
$("textarea#code").val( $('body').html() );
});
第一次更改后,一切正常,HTML 更改成功,但完成此操作后,用户无法第二次更改 HTML!换句话说,JavaScript 没有加载。
这是我的片段:
$("textarea#code").val( $('body').html() );
$("textarea#code").change(function(){
$('body').html( $(this).val() );
$("textarea#code").val( $('body').html() );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<center>
Hello!<br />
<textarea cols="65" rows="10" id="code"></textarea>
</center>
</body>
最佳答案
您需要授权:
$("textarea#code").val($('body').html());
$(document).change("textarea#code", function (e) {
$('body').html($(e.target).val());
$("textarea#code").val($('body').html());
});
关于javascript - HTML 更改后加载 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955170/