Tim*_*Tim 13 javascript jquery
我正在使用jQuery在扩展项目时附加一些JavaScript.
但是jQuery函数内部的JavaScript代码导致其余的jQuery代码爆炸.
$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</script>');
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
The*_*iot 20
对于附加的JavaScript问题,请使用以下命令:
var str="<script>alert('hello');";
str+="<";
str+="/script>";
$(this).parent('li').find('.moreInfo').append(str);
Run Code Online (Sandbox Code Playgroud)
否则,更好的选择是连接li的点击事件:
$("ul#myList li").click(function(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "url to your content page",
data: "{}",//any data that you want to send to your page/service
dataType: "json",
success: function(msg) {
$(this).find('.moreInfo').append(msg);
}
});
});
Run Code Online (Sandbox Code Playgroud)
其实我不知道你用的是什么语言.如果您使用的是ASP.NET,请阅读Dave的这篇博客.