[英]function in javascript not working when called
I have a javascript code that works by removing the first and the last line of it. 我有一个javascript代码,通过删除它的第一行和最后一行。
Please take a look at JSFiddle 请看一下JSFiddle
for people who wants to see it in here, here is my html: 对于想在这里看到它的人,这是我的html:
<input id="search" onclick="search()" type="button" value="Search"/>
my javascript : 我的javascript:
function search() {
var search = document.getElementById('search');
var int = setInterval(function() {
if (search.value.length == 6)
search.value = 'Searchi';
else if (search.value.length == 7)
search.value = 'Searchin';
else if (search.value.length == 8)
search.value = 'Searching';
else {
search.value= 'Search';
}
//clearInterval( int ); // at some point, clear the setInterval
}, 500);
}
I want the function to work only when I click the button. 我希望该功能仅在我单击按钮时才能工作。
You've selected jQuery
in jsfiddle.net which by default causes the site to wrap your whole code in a document.ready
handler. 你已经在jsfiddle.net中选择了
jQuery
,它默认导致网站将你的整个代码包装在document.ready
处理程序中。
The result is that your search
function becomes a local function within that wrapper, and not a global variable as required by a DOM0 onclick
handler. 结果是您的
search
函数成为该包装器中的本地函数,而不是DOM0 onclick
处理程序所需的全局变量。
Set the jsfiddle options to "no wrap (body)" and "No-Library (pure js)" to turn off that functionality. 将jsfiddle选项设置为“no wrap(body)”和“No-Library(pure js)”以关闭该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.