English 中文(简体)
jquery.load() function not calling the jquery in embedded file
原标题:

I have 2 html files : test1 and test2

test1.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" src="lib/jquery-1.3.2.min.js"/></script>
<script language="javascript" type="text/javascript">

$(document).ready(function(){


   $( #content ).empty().load( test2.html );

});


</script>


</head>

<body>
<div id="content"></div>

</body>
</html>

test2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="javascript" type="text/javascript" src="lib/jquery-1.3.2.min.js"/></script>
<script language="javascript" type="text/javascript">

$(document).ready(function(){


  alert(202);

});

</script>


</head>

<body>
test
</body>
</html>

When I run test1 it only shows "test" and not the alert.So it cannot take in the embedded jquery. So how can I make it take? Please help.

问题回答

The load function always filters out everything from the referenced HTML except the body element on load. Try moving your script into the body element and see if that helps.

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". Default selector "body>*" always applies. If the URL contains a space it should be escape()d. See the examples for more information.

Note that you can apply a selector on load that does more extensive filtering as well.

You need to put the script into the <body> on the page that you are loading.

Aleternatively, you might consider using the callback of the load() function to show the alert

$(document).ready(function(){

   $( #content ).empty().load( test2.html , function() { alert(202); });

});




相关问题
newbie jQuery plugin issue

I m just getting into jQuery plugins and I wanted to do a sort of hello world exercise with a barebones object-oriented plugin template. But I can t get the console.log statement in the setup() ...

Adding reverse highlighting to the jQuery image map plugin?

I m using the jQuery Map Hilighter plugin, but instead of fading a dark patch over each area, I would like to reverse this and instead make the surrounding area dark, keeping the hovered area the same ...

jQuery Address Plugin - Not allowing loading ajax?

I am trying to test the jQuery Address Plugin and it seems to not allow ajax to work in the change function. I am using: $.address.change(function(event) { $( #content ).load(event.value+ #...

JQuery validation plugin - error highlight problem

I have a form with two input textboxes, and I have included jQuery validation rules for both: <script src="../../Scripts/jquery-validate/jquery.validate.js" type="text/javascript"></script&...

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Impromptu plugin for Alert Boxes

When we use Javascript prompt Box something like this var ur_name = prompt("Enter Name"); We will have two types of values in this case: Value entered by user. Second VALUE can be NULL, when user ...

热门标签