![](/img/trans.png)
[英]Why does my java script work perfectly in jsFiddle but not in any browser?
[英]Why does it work in jsfidde but not on my browser?
请查看此jsfiddle:
在我的jsfiddle中效果很好
[在输入中输入文本时,会出现框]
但无法在我的浏览器上使用,但我使用的是safari,firefox和chrome最新版本。
有什么事?
HTML / JAVASCRIPT代码:
<html>
<head>
<script type="text/javascript" src="jquery-1.7.js"></script>
<script>
$(document).ready(function () {
(function watchInputForChanges(){
var hasInput = $('.input1').val() != "";
$('.box')[hasInput ? 'show' : 'hide']();
setTimeout(watchInputForChanges, 100);
})();
});
</script>
<link href="cloud.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="center1">
<form>
<input type="text" class="input1" autofocus="focus" />
</form>
</div>
<br><br>
<div class="center1">
<div class="box">f</div>
</div>
</body>
</html>
我相信您需要添加以下内容:
$(document).ready(function () {
//function name does not exist outside this scope
(function watchInputForChanges(){
var hasInput = $('.input1').val() != "";
$('.box')[hasInput ? 'show' : 'hide']();
setTimeout(watchInputForChanges, 100);
})();
});
很抱歉没有回答直接的问题(简单地说:“为什么这不能在小提琴之外发挥作用?”),但我无法抗拒:不确定为什么要轮询更改;为什么? 您可以将侦听器绑定到元素。 例如(在文档准备功能中...)
在HTML本身中:
<script src="/scripts/jquery.js"></script> <!--or wherever your script source is!!-->
然后,如果要将脚本放入同一页面(而不是外部)的标记中:
<script>
$(document).ready(function () {
(function() {
$input = $('.input1');
$box = $('.box');
$input.on('keypress input paste', function() {
if ($box.not(':visible') && $input.val() != "") {
$box.show();
} else {
$box.hide();
}
});
})();
});
</script>
捕获手动输入或粘贴的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.