Jquery Keyup() 示例在服务器上不起作用

发布于 2024-12-03 09:47:54 字数 415 浏览 1 评论 0原文

我尝试在我进行的一些搜索中使用keyup功能,但是对于服务器上的文件并通过IE8和FF访问页面,keyup功能似乎不起作用。为了测试它,我借用了 jQuery 网站示例中的代码,并将其放在服务器上(Windows Server 2003 R2)。但是,这个示例也不起作用(http://api.jquery.com/keyup/)。我注意到它有一行

我最初的想法是,“我的不是”无法工作,因为我缺少此事件文件!”但谷歌搜索后我认为情况并非如此。在我的代码中,我链接到下载的 jQuery 副本,并且我还尝试通过 googleapi 的副本链接它,但两次都没有成功。关于为什么 keyup 功能可能无法在服务器上运行的任何想法?

I am trying to use the keyup function in a little search I made, but with the files on the server and accessing the page through IE8 and FF, the keyup function doesn't seem to work. To test it I borrowed the code from the jQuery websites example put it on the server (it's Windows Server 2003 R2). However, this example also isn't working(http://api.jquery.com/keyup/). I noticed it has the line

<script type="text/javascript" src="/scripts/events.js"></script>

and my initial thought was, "mine isn't working because I'm missing this events file!" but after googling I'm thinking this isn't the case. In my code I'm linking to a downloaded copy of jQuery, and I've also tried linking it through the googleapi's copy, no luck either time. Any thoughts about why the keyup function might not be working on the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-12-10 09:47:54

该页面上的代码似乎有点损坏。具体来说,它似乎使用了最新 jQuery 中缺少的 print 函数(也许它包含在缺少的 events.js 脚本中?)

我做了一些小的更改制作一个工作版本(请注意,我使用的是 Google 托管的 jQuery;您可以将其指向您的本地版本,它应该仍然可以正常工作。)

<!DOCTYPE html>
<html>
<head>
  <style>
fieldset { margin-bottom: 1em; }
input { display: block; margin-bottom: .25em; }
#print-output {
  width: 100%;
}
.print-output-line {
  white-space: pre;
  padding: 5px;
  font-family: monaco, monospace;
  font-size: .7em;
}

</style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
</head>
<body>
  <form>
  <fieldset>
    <label for="target">Type Something:</label>
    <input id="target" type="text" />
  </fieldset>
</form>
<button id="other">
  Trigger the handler
</button>
<div id="output"></div>
<script>
var xTriggered = 0;
$('#target').keyup(function(event) {
  if (event.keyCode == '13') {
     event.preventDefault();
   }
   xTriggered++;
   var msg = 'Handler for .keyup() called ' + xTriggered + ' time(s).<br />';
   $('#output').append(msg);
});

$('#other').click(function() {
  $('#target').keyup();
});</script>

</body>
</html>

在这个更改后的版本中,我使用 jQuery 的 append()< /code> 输出函数将调试消息添加到我在文本框下方添加的新 div 中。当您在文本框中键入内容时,您应该会看到在那里注册了按键事件。

The code on that page seems a little broken. Specifically, it seems to use a print function that's absent from the latest jQuery (perhaps it's included in that missing events.js script?)

I've made some small changes to make a working version (note I'm using the Google-hosted jQuery; you can point that at your local version instead and it should still work fine.)

<!DOCTYPE html>
<html>
<head>
  <style>
fieldset { margin-bottom: 1em; }
input { display: block; margin-bottom: .25em; }
#print-output {
  width: 100%;
}
.print-output-line {
  white-space: pre;
  padding: 5px;
  font-family: monaco, monospace;
  font-size: .7em;
}

</style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
</head>
<body>
  <form>
  <fieldset>
    <label for="target">Type Something:</label>
    <input id="target" type="text" />
  </fieldset>
</form>
<button id="other">
  Trigger the handler
</button>
<div id="output"></div>
<script>
var xTriggered = 0;
$('#target').keyup(function(event) {
  if (event.keyCode == '13') {
     event.preventDefault();
   }
   xTriggered++;
   var msg = 'Handler for .keyup() called ' + xTriggered + ' time(s).<br />';
   $('#output').append(msg);
});

$('#other').click(function() {
  $('#target').keyup();
});</script>

</body>
</html>

In this changed version, I'm using jQuery's append() function to output the debugging messages into a new div I've added below the text box. When you type into the text box, you should see the key press events being registered there.

红ご颜醉 2024-12-10 09:47:54

您正在观看的元素是否在页面加载后动态生成?如果是这样,您需要使用 live:

$("#element").live('keyup',function(e) {
  var keycode = e.keyCode ? e.keyCode : e.which;
});

否则尝试:

$("element").keyup(function(e) {
  var keycode = e.keyCode ? e.keyCode : e.which;
});

另外,请确保您选择正确的元素(输入框?)并仔细检查选择器是否正确。在不同的浏览器中,无论是否处理 e.keyCode,都可能会出现一些问题。如果 e.keyCode 不存在,上面的示例还会检查 e.which 中的键代码值。

Does the element you're watching get generated dynamically, after page load? If so you'll need to use live:

$("#element").live('keyup',function(e) {
  var keycode = e.keyCode ? e.keyCode : e.which;
});

Otherwise try:

$("element").keyup(function(e) {
  var keycode = e.keyCode ? e.keyCode : e.which;
});

Also, make sure you are selecting the proper element (input box?) and double check the selector is correct. Some problems might arise in different browsers, handling e.keyCode, or not. The above example also checks for a key code value in e.which, if e.keyCode does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文