假设我具有以下结构,仅是示例:
(function number1( $ ) {
$.fn.abcd = function( options ) {
heyyou.find(".123").css({});
heyyou.find(".456").css({});
var delay = 1000;
var site = "http://url";
setTimeout(function(){ window.location.href = site; },delay);}
$("#button").on('click', function(){ window.location.href = site;});
return this;
}( jQuery ));
而且我认为我需要做的是这样的:
//part1
(function number1( $ ) {
$.fn.abcd = function( options ) {
heyyou.find(".123").css({});
heyyou.find(".456").css({});
return this;
}( jQuery ));
//part2
(function number1( $ ) {
$.fn.abcd = function( options ) {
var delay = 1000;
var site = "http://url";
setTimeout(function(){ window.location.href = site; },delay);}
$("#button").on('click', function(){ window.location.href = site;});
return this;
}( jQuery ));
我想在这里实现的是:“ part1”将位于一个js文件中,该文件将放置在标头中。 “ part2”是我以后需要在DOM上从<script></script>
调用的函数。问题是两个部分都使用相同的主要功能,一个正在继续另一个。
例如:
<script src="part1.js"></script>
<body>
<script>//part2</script>
<button id="button">Do something</button>
</body>
因此,基本上,我希望该功能在js文件中启动,然后从另一部分结束它。我不确定如何解释这一点,只是我需要分别调用var site
,因为我要在不同页面上使用相同的脚本,所以我需要每个页面都用var site
来调用一个不同的值。
基本上,它是一个重定向脚本,所以我不能对每个页面使用相同的重定向URL。
编辑:
结构需要像这样:
mainjs.js:
<header>
<script>
//part1
(function number1( $ ) {
$.fn.abcd = function( options ) {
heyyou.find(".123").css({});
heyyou.find(".456").css({});
return this;
}( jQuery ));
</script>
</header>
第1页:
//part2
var site google
第2页:
//part2
var site bing
第3页:
//part2
var site yahoo
“ // Part1”首先执行,并在每个页面中执行。“ // part2”稍后执行,并且每个页面在“ // part2”函数内部将有不同的URL重定向到。
回复Ehsan:
它必须是这样的:
<script type="text/javascript" >
function function1(site) {
//redirect to site
alert(site);
}
</script>
<script type="text/javascript" >
function function2() {
// Do your css thing here.
site = "http://somethingelse";
function2(site);
}
</script>
<button onclick="function2()">Click me</button>
假设我具有以下结构,仅举一个例子:(function number1($){$ .fn.abcd = function(options){heyyou.find(“。123”)。css({}); .. 。