addEventListener不起作用 [英] addEventListener not working

查看:603
本文介绍了addEventListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个的新手,这是我的问题我有 html文件和扩展的JavaScript文件

html文件有一个textarea 我需要当我输入textarea显示为带有该文本的警告框时获取每个字母(测试目的)

i使用 addEventListener 获取活动但是不行吗



你能否在我的代码中显示错误的地方



我的html文件看起来像这样



 <  !DOCTYPE     html  >  
< html >
< head >

< / head >
< 正文 >
< ; script src < span class =code-keyword> =
script.js > < span class =code-keyword>< / script >
< form 名称 = txtBox id = txtBox >

< span class =code-keyword>< textarea style = font -size:12pt; width:500px; id = box1 rows < span class =code-keyword> = 7 > < / textarea >

< / form >
< / body >
< / html > ;





我的扩展JavaScript文件看起来像这样



<前lang =Javascript> function textfunc()
{
var text = document .txtBox.box1.value;
alert(text);
// 其余功能在这里
}

document .getElementById( txtBox.box1).addEventListener( onkeyup,textfunc, false );

解决方案

你没有得到它。现在,你在哪里得到这个 document.txtBox.box1



这样做:

 textBox =  document  .getElementById(  txtBox.box1\" );  //  现在可以通过以下功能访问 

function someTest(){
alert(textBox.value);
}

textBox.addEventListener( onkeyup,someTest , false );





我明白这仅用于测试。在现实生活中,你可以使用带有参数的事件处理程序,或者在元素typw上设置处理程序,或者更好的jQuery方法。

如果你需要学习jQuery(强烈推荐),请请参阅:

http://en.wikipedia.org/wiki/JQuery [ ^ ],

http://jquery.com [ ^ ],

http://learn.jquery.com [ ^ ],

http://learn.jquery.com/using-jquery-core [ ^ ],

http://learn.jquery.com/about-jquery / how-jquery-works [ ^ ](从这里开始)。



要在HTML中添加处理程序,请使用类似

 <  输入    type   =  text    onkeyup   =  someTest();    /  >  





-SA


i'm new to this and this is my Question i have html file and extended JavaScript file
html file has one textarea i need get every letter when i type textarea show as a alertbox with that text (testing purposes)
i use the addEventListener to get event but it's not works

can you please show where the wrong in my code

my html file looks like this

<!DOCTYPE html>
<html>
<head>

</head>
<body>
         <script src="script.js"></script>
	<form name="txtBox" id="txtBox">
						
        <textarea  style="font-size: 12pt; width: 500px;" id="box1" rows="7"></textarea>
                        
    </form>
</body>
</html>



My Extended JavaScript file looks like this

function textfunc()
{
var text = document.txtBox.box1.value;
alert(text);
// rest of function is here
}

document.getElementById("txtBox.box1").addEventListener("onkeyup",textfunc,false);

解决方案

You are no getting it. Now, where did you get this document.txtBox.box1?

Do this:

textBox = document.getElementById("txtBox.box1"); // now it's accessible by the function below

function someTest() {
    alert(textBox.value);
}

textBox.addEventListener("onkeyup", someTest, false);



I understand this is just for testing. In real life, you can use the event handler with a parameter, or setup the handler on the element typw, or, even better jQuery approach.
If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

To add a handler in HTML, use something like

<input type="text" onkeyup="someTest();" />



—SA


这篇关于addEventListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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