gpt4 book ai didi

javascript - 停止通过 x-editable 输入 <script>

转载 作者:行者123 更新时间:2023-12-03 07:36:46 25 4
gpt4 key购买 nike

我们使用x-editable用于快速编辑行。我们注意到一个小错误。当 <script>标签被输入到将被执行的可编辑输入中。

<div class="test"></div>
$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});

例如,如果 newValue字符串值为 <script>alert('hello')</script> ,然后我们看到 alert消息显示为“你好”。

我们怎样才能阻止这种行为?

最佳答案

将输入中的字符串“script”替换为“code”...这样它将输出为“text”。也许是这样的……

$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
//gi: Perform a global, case-insensitive replacement:
newValue = newValue.replace(/script/gi, "code");
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});

JavaScript String replace() Method

关于javascript - 停止通过 x-editable 输入 &lt;script&gt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com