jQuery-将JavaScript附加到Div [英] jQuery - Append JavaScript to a Div

查看:84
本文介绍了jQuery-将JavaScript附加到Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 附加一些

I'm using jQuery to append some JavaScript when an item is expanded.

但是jQuery函数中的JavaScript代码正在导致其余jQuery代码爆炸.

But the JavaScript code inside of the jQuery function is causing the rest of the jQuery code to explode.

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

我该怎么做才能解决这个问题?

What can I do to solve this problem?

推荐答案

对于附加的JavaScript问题,请使用以下方法:

For the appending JavaScript problem use this:

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

$(this).parent('li').find('.moreInfo').append(str);

否则,更好的选择是连接 li的点击事件:

Otherwise a better option will be to wire-up li's click event:

 $("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);
            }
        });
 });

实际上我不知道您使用的是哪种语言.如果您使用的是 ASP.NET ,请阅读此

Actually I don't know what language are you using. If you are using ASP.NET please read this blog by Dave.

这篇关于jQuery-将JavaScript附加到Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆