jQuery-将JavaScript附加到Div

jQuery-将JavaScript附加到Div,javascript,jquery,Javascript,Jquery,我使用在项目展开时附加一些 但是jQuery函数内部的JavaScript代码导致jQuery代码的其余部分爆炸 $(this).parent('li').find('.moreInfo').append('<script>alert("hello");</script>'); $(this).parent('li').find('.moreInfo').append('alert(“hello”);'); 如何解决此问题?对于附加JavaScript问题,请使用以下

我使用在项目展开时附加一些

但是jQuery函数内部的JavaScript代码导致jQuery代码的其余部分爆炸

$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</script>');
$(this).parent('li').find('.moreInfo').append('alert(“hello”);');

如何解决此问题?

对于附加JavaScript问题,请使用以下命令:

var str="<script>alert('hello');";
str+="<";
str+="/script>";

$(this).parent('li').find('.moreInfo').append(str);
实际上我不知道你在用什么语言。如果您正在使用,请阅读Dave的文章。

$(文档).ready(函数()){ $(“ul”)。每个(函数(){ var _list=这个; $(_列表)。查找(“li”)。单击(函数(){ var _listItem=此; 制作动画({height:20},300); $(this.append(“+”$(“.resultspaga”).text(‘我是通过JS注入的,不会弄乱你写的其他JS,因为我是个好孩子’;“+”); }); }); });
尝试在
/script
之前添加
\
,它会解决您的问题。

可以这样尝试

$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</' + 'script>');
$(this).parent('li').find('.moreInfo').append('alert(“hello”);');

请发布html并说明添加脚本的目的好吗?html在这里不太适用,javascript的目的是从远程源获取信息。但是我宁愿按需拉取信息,而不是在加载时拉取我站点上70多个项目的信息。但是你为什么要将javascript附加到div中呢?您可以在单击时获取节点的内容,并将其附加到div或div中。因为如果页面上有javascript,则会导致来自远程源的请求,这意味着由于请求的总数,加载时间会变慢。通过按需拉动,我不会看到相同的缓慢加载时间。
$(document).ready(function() {
        $("ul").each(function() {
            var _list = this;
            $(_list).find("li").click(function() {
                var _listItem = this;
                $(this).animate({ height: 20 }, 300);
                $(this).append("<script type='text/javascript'>" + "$('.resultsParag').text('I am injected via JS and will not mess up the other JS you have written cause I am a good boy');" + "</" + "script>");
            });
        });
    });
$(this).parent('li').find('.moreInfo').append('<script>alert("hello");</' + 'script>');