是不是不喜欢使用内联JavaScript? [英] Is it preferrable to not use inline javascript?
问题描述
哪种代码更受欢迎?
代码A:
< script type =" text / javascript">
函数someFunc(){
//东西
}
< / script>
< body>
< div onmouseover =" someFunc()">
< a href =" ..."> link< / a>
< / div>
< / body>
代码B:
< script type =" text / javascript">
function someFunc(){
// stuff
}
函数init(){
var div1 = document.getElementById(''div1'');
div1.onmouseover = someFunc; < br $>
}
< / script>
< body>
< div id =" div1" >
< a href =" ..."> link< / a>
< / div>
< / body>
可以说,代码B是首选,因为它完全分开了
t他是来自HTML的Javascript。你觉得怎么样?
Which code is preferrable?
Code A:
<script type="text/javascript">
function someFunc() {
// stuff
}
</script>
<body>
<div onmouseover="someFunc()">
<a href="...">link</a>
</div>
</body>
Code B:
<script type="text/javascript">
function someFunc() {
// stuff
}
function init() {
var div1 = document.getElementById(''div1'');
div1.onmouseover = someFunc;
}
</script>
<body>
<div id="div1">
<a href="...">link</a>
</div>
</body>
It could be argued that Code B is preferrable because it completely separates
the Javascript from the HTML. What do you guys think?
推荐答案
哎呀,代码B应该有这个身体标签:
< body onload =" init()">
Oops, code B should have this body tag:
<body onload="init()">
de ******* @ no.spam.com 写道:
de*******@no.spam.com wrote:
哎呀,代码B应该有这个正文标签:
< body onload =" init()">
Oops, code B should have this body tag:
<body onload="init()">
几乎不分开它然后呢?
window.onload = function(){init(); }
-
David Dorward< http://dorward.me.uk/>
Hardly seperates it then does it?
window.onload = function() { init(); }
--
David Dorward <http://dorward.me.uk/>
>
delerious写道:
delerious wrote:
可以认为代码B是可取的,因为它完全将Javascript与HTML分开。你觉得怎么样?
It could be argued that Code B is preferrable because it completely separates
the Javascript from the HTML. What do you guys think?
我更喜欢代码C:
< script type =" text / javascript" >
函数someFunc(){
// stuff
}
document.getElementById(''div1 '')。onmouseover = someFunc;
< / script>
< body>
< div id =" div1" >
< a href =" ..."> link< / a>
< / div>
< / body>
甚至更好的代码D:
< script type =" text / javascript" src =" behaviour.js">< / script>
< body>
< div id =" div1">
< a href =" ..."> link< / a>
< / div>
< / body>
其中behaviour.js包含:
函数someFunc(){
// stuff
}
document.getElementById(''div1'')。onmouseover = someFunc;
使用代码D,javascript变成一个简单的drop-在文件中(像CSS一样)
-
托比A Inkster BSc(荣誉)ARCS
联系我 - http://www.goddamn.co.uk/tobyink/?page= 132
I prefer code C:
<script type="text/javascript">
function someFunc() {
// stuff
}
document.getElementById(''div1'').onmouseover = someFunc;
</script>
<body>
<div id="div1">
<a href="...">link</a>
</div>
</body>
Or even better code D:
<script type="text/javascript" src="behaviour.js"></script>
<body>
<div id="div1">
<a href="...">link</a>
</div>
</body>
where behaviour.js contains:
function someFunc() {
// stuff
}
document.getElementById(''div1'').onmouseover = someFunc;
With code D, the javascript becomes a simple drop-in file (like CSS is)
--
Toby A Inkster BSc (Hons) ARCS
Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
这篇关于是不是不喜欢使用内联JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!