javascript 如何在没有按钮点击的情况下调用javascript函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22422323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call javascript function without button click?
提问by Sreeraj
I have seen some pages in which we can input in textarea and correspondingly an output gets displayed for it no button clicks in between , see an example here http://ntools.infoexpo.in/p/html-to-xml-encoder_4042.html
我已经看到一些页面,我们可以在 textarea 中输入,并相应地显示输出,中间没有按钮点击,请参阅此处的示例http://ntools.infoexpo.in/p/html-to-xml-encoder_4042。 html
How to apply this type in the below code? html:
如何在下面的代码中应用这种类型?html:
<textarea id="source" placeholder="Text Entry."></textarea>
<p id="result"> </p>
Javascript
Javascript
function myFunction()
{
var str = document.getElementById('source').value;
var len = str.length;
document.getElementById("result").innerHTML="<textarea>"+len+"</textarea>";
}
回答by MusicLovingIndianGirl
If you want to call in on load of your page try this
如果你想在页面加载时调用,试试这个
<body onLoad="yourFunctionName();">
回答by nanobash
I'd suggest you to use addEvent
,removeEvent
function approaches due to cross browser issues.
由于跨浏览器问题addEvent
,我建议您使用,removeEvent
函数方法。
addEvent
function is capable of to add events on elements, while removeEvent
remove them
addEvent
函数能够在元素上添加事件,同时removeEvent
删除它们
if ( document.addEventListener ) { // if addEventListener provided
this.addEvent = function(elem, type, fn) {
elem.addEventListener(type, fn, false);
return fn;
};
this.removeEvent = function(elem, type, fn) {
elem.removeEventListener(type, fn, false);
};
} else if ( document.attachEvent ) { // if attachEvent provided
this.addEvent = function(elem, type, fn) {
var bound = function() {
return fn.apply(elem, arguments);
};
elem.attachEvent("on" + type, bound);
return bound;
};
this.removeEvent = function (elem, type, fn) {
elem.detachEvent("on" + type, fn);
};
}
For example if you have HTML
例如,如果您有HTML
<textarea id="source" placeholder="Text Entry."></textarea>
<textarea id="result" placeholder="Text Entry." disabled="disabled"></textarea>
JavaScript
JavaScript
addEvent(document.getElementById("source"),"keydown",function(){ // event will occur on keyup
document.getElementById("result").innerHTML = this.value; // this refers to textarea element
});
PS.Check outattachEventand addEventListenermethods
附注。检出attachEvent及addEventListener方法
回答by a.ras2002
I recommend you to use the onchange event, so everytime the user made some change on the text field it will trigger the function.
我建议您使用 onchange 事件,因此每次用户对文本字段进行一些更改时,它都会触发该功能。
HTML will be something like this
HTML 将是这样的
<textarea id="source" onchange="myFunction();" placeholder="Text Entry."></textarea>
<p id="result"> </p>
JavaScript code
JavaScript 代码
function myFunction() {
var str = document.getElementById('source').value;
var len = str.length;
document.getElementById("result").innerHTML="<textarea>"+len+"</textarea>";
}
回答by Vamshavardhan G
document.getElementById('source').onchange(function() {
// Write your code here . It will execute when your changes the text in textarea
})
go through the link http://www.w3schools.com/jsref/event_onchange.asp
回答by Dude Lebowski
use onchange event:
使用 onchange 事件:
object.onchange=function(){SomeJavaScriptCode};
回答by Jixun
<textarea cols="40" onchange="do_encode(this)" onkeyup="do_encode(this)" ...>
<textarea cols="40" onchange="do_encode(this)" onkeyup="do_encode(this)" ...>
Every time user type/paste anything will trigger onchangeand onkeyupevent.
每次用户键入/粘贴任何内容都会触发onchange和onkeyup事件。
There's a full list of events reference on MDN.