我们使用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 - 停止通过 x-editable 输入 <script>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579032/