简体   繁体   English

使用Jquery将HTML插入DIV

[英]Insert HTML into DIV with Jquery

This is one of my first jquery scripts. 这是我的第一个jquery脚本之一。 I want to expand a DIV and load some external html into it. 我想扩展DIV并将一些外部html加载到其中。 This is the code I have so far: 这是我到目前为止的代码:

http://jsfiddle.net/spadez/uhEgG/5/ http://jsfiddle.net/spadez/uhEgG/5/

This is the code I have: 这是我的代码:

$(document).ready(function () {
var loadUrl = http://sheldonbrown.com/web_sample1.html

    $("#close").click(function () {
        $("#country_slide").hide();
    });

    $("#country").click(function () {
        $("#country_slide").show();
        $("#country_slide").html(ajax_load).load(loadUrl);
    });

});

The expanding did work but now it doesn't since adding the Ajax code. 扩展确实可以,但是自从添加Ajax代码以来,它一直没有。 Can anyone show me hwere I am going wrong, and ideally highlight any thing I am doing wrong. 谁能告诉我我要去哪里错了,最好是突出我做错了什么。 Thank you. 谢谢。

Just replace country click with this 只需替换国家点击与此

$("#country").click(function () {
        $("#country_slide").show();
        $("#country_slide").load(loadUrl);
    });

and add quotes on url 并在网址上加上引号

var loadUrl = "http://sheldonbrown.com/web_sample1.html";

Fixed it for you.. 已为您修复。

var loadUrl = 'http://sheldonbrown.com/web_sample1.html';

http://jsfiddle.net/APkHN/1/ http://jsfiddle.net/APkHN/1/

your URL was not a string, and it was missing an end brace. 您的网址不是字符串,并且缺少结尾括号。

put your url inside '' 将您的网址放入''

see it working here --> http://jsfiddle.net/uhEgG/8/ 看到它在这里工作--> http://jsfiddle.net/uhEgG/8/

Also, you might need to delegate close like this ( As you are replacing html inside #country_slide which contains your #close ) 此外,您可能需要委派接近这样的( 正如您要更换里面的html #country_slide其中包含你的#close

$("#country_slide").on('click','#close',function () {
        $("#country_slide").hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM