且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

addEventListener不起作用

更新时间:2023-08-22 22:14:10

你没有得到它。现在,你在哪里得到这个 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