JavaScript OnChange监听器在HTML标记中的位置 [英] JavaScript OnChange Listener position in HTML markup

查看:78
本文介绍了JavaScript OnChange监听器在HTML标记中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在< / form> 标记之后定位脚本,侦听器可以正常工作,但是如果将脚本放在脚本中,我在使用OnChnage侦听器时会遇到问题在< head> 标签内,它无法工作。

在我的网站上,我只能在< head> 标签内找到我能做的任何事情使脚本在< head> 标签内运行?



在此配置中,脚本不起作用

 < head> ; 
< script type =text / javascript>
if(window.addEventListener){
document.getElementById('address')。addEventListener('change',loadXMLDoc,false);
} else if(window.attachEvent){
document.getElementById('address')。attachEvent(onchange,loadXMLDoc);


函数loadXMLDoc(){
alert('worked');
}
< / script>
< / head>

< body>
< form>

< input id =addressname =addresstype =text/>
< input id =testname =testtype =text/>

< / form>


解决方案


$ b

 < head> 
< script type =text / javascript>
window.onload = function(){
if(window.addEventListener){
document.getElementById('address')。addEventListener('change',loadXMLDoc,false);
} else if(window.attachEvent){
document.getElementById('address')。attachEvent(onchange,loadXMLDoc);


函数loadXMLDoc(){
alert('worked');
}
}
< / script>
< / head>


I am having problems getting the OnChnage Listener to work, if i position the script after the </form> tag the listener works fine but if i place the script within the <head> tags it fails to work.

On my site i can only have the script within the <head> tags is there anything I can do to make the script runn within the <head> tags?

in this configuration the script does not work

<head>
<script type="text/javascript">
if(window.addEventListener) {
    document.getElementById('address').addEventListener('change', loadXMLDoc, false);
} else if (window.attachEvent){
    document.getElementById('address').attachEvent("onchange", loadXMLDoc);
}

function loadXMLDoc(){
   alert('worked');
}
</script>
</head>

<body>
<form>

<input id="address" name="address" type="text"/>
<input id="test" name="test" type="text"/>

 </form>

解决方案

Put it this way:

<head>
<script type="text/javascript">
window.onload= function () {
    if(window.addEventListener) {
        document.getElementById('address').addEventListener('change', loadXMLDoc, false);
    } else if (window.attachEvent){
        document.getElementById('address').attachEvent("onchange", loadXMLDoc);
    }

    function loadXMLDoc(){
       alert('worked');
    }
}
</script>
</head>

这篇关于JavaScript OnChange监听器在HTML标记中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆