好的,这是我第一次问问题,尽管我已经使用该网站很长时间了。我似乎找不到我想要的东西。
我有一个文档就绪功能,其中的某些部分希望我可以“导出”,因此不需要在每一页上都重复这些内容。有点像ASP,包括页眉,页脚甚至是主容器。这可行吗?请看一下我的意思:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Some Title</title>
<link rel="styleSheet" href="somePath/myCssFile.css" type="text/css" media="screen" />
<script src="../somePath/jquery.min.js" type="text/javascript"></script>
<script src="../somePath/my_own_JS_stuff.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
// this part is unique to THIS page
$('#myUniqueDiv').delay(1000).fadeIn(500);
$(document).attr("title", "MisterKay.com | " + someArray[parseInt(a) + parseInt(b)]);
$('#myUniqueDiv').hover(function(){
// some code here
}, function(){
// some more code here
});
// This part is repeated on all pages of the site.
// I wish I could take this part out of here, put it in a file and just call the file, like a virtual file in ASP.
// That way, if I make a modification, I could do it once for the entire site, instead for doing it for each page.
// I cannot put it in a separate JS file either; for some reason it gives me an error.
// My guess is this happens because this is INSIDE of the document-ready-function.
// But I also cannot put it outside of the document-ready-function.
$('#someRepeatedDiv').cycle({
fx: 'fade',
speed: 1000,
timeout: 3000
});
$('#someRepeatedInputBox').blur(function(){
var sVal = $(this).val();
if(sVal == ""){
$(this).val(someArray[a]);
$(this).css('color','#999');
}
});
// End of repeated section of the document-ready-function
}); // End of document-ready-function
</script>
</head>
<body>
<!-- #include virtual="header.html" -->
<!-- #include virtual="main.html" -->
<!-- #include virtual="footer.html" -->
</body>
</html>
如何将需要在每页上重复的部分放入某个外部文件,并仍由文档就绪功能读取?
再次感谢您的解答!
您可以尝试jQuery $ .getScript():
Load a JavaScript file from the server using a GET HTTP request, then execute it.
例子:
$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {
console.log( data ); // Data returned
console.log( textStatus ); // Success
console.log( jqxhr.status ); // 200
console.log( "Load was performed." );
});
有关更多详细信息:http : //api.jquery.com/jquery.getscript/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句