Javascript 函数会导致 HTML 页面重新加载:为什么?

发布于 2024-07-25 19:44:53 字数 1156 浏览 5 评论 0原文

新手问题...

目标:

  • 我打算有一个 HTML 文本输入字段作为一种命令行输入。

  • 无序的 HTML 列表显示最近的 5 个命令。 单击此列表中最后的命令之一应使用相应的命令填充命令行输入文本字段(以便重新执行或修改它)。

  • 无序 HTML 列表包含结果集。 单击此列表中的 ID 应该将相应的 ID 带入命令行输入文本字段。

在 HTML (DHTML) 中: 按预期工作:单击链接时,命令行输入文本字段将填充最近的命令。

<li><a href="#" id="last_cmd_01" onclick="document.getElementById('cli_input').value = document.getElementById('last_cmd_01').firstChild.nodeValue;document.getElementById('cli_input').focus()">here would be one of the recent commands</a></li>

在 Javascript 文件中: 无法按预期工作:单击链接时,命令行输入文本字段会填充相应的值(应该如此),但随后似乎正在重新加载完整的 HTML 页面,文本输入字段和所有动态填充的列表都会变空。

    function exec_cmd(cli_input_str) {
// a lot of code ...
// the code that should provide similar behavior as onclick=... in the DHTML example
$('.spa_id_href').click(function(){document.getElementById('cli_input').value = document.getElementById('cli_input').value + this.firstChild.nodeValue;});
}

现在的问题: 除了潜在的 Javascript(语法)错误之外,还有什么可能导致浏览器重新加载页面?

Newbie question...

The objective:

  • I intend to have an HTML text input field as a kind of command line input.

  • An unordered HTML list shows the 5 most recent commands. Clicking on one of the last commands in this list should populate the command line input text field with the respective command (in order to re-execute or modify it).

  • An unordered HTML list contains a result set. Clicking on an ID in this list should bring the respective ID into the command line input text field.

In HTML (DHTML):
Works as expected: when clicking on the the link the command line input text field is populated with a recent command.

<li><a href="#" id="last_cmd_01" onclick="document.getElementById('cli_input').value = document.getElementById('last_cmd_01').firstChild.nodeValue;document.getElementById('cli_input').focus()">here would be one of the recent commands</a></li>

In Javascript file:
Doesn't work as expected: when clicking on the the link the command-line-input-text-field gets populated with the respective value (as it should), BUT then it seems like the full HTML page is being reloaded, the text input field and all dynamically populated lists become empty.

    function exec_cmd(cli_input_str) {
// a lot of code ...
// the code that should provide similar behavior as onclick=... in the DHTML example
$('.spa_id_href').click(function(){document.getElementById('cli_input').value = document.getElementById('cli_input').value + this.firstChild.nodeValue;});
}

Now the Question:
Besides a potential Javascript (syntax) error, what could cause the browser to reload the page?

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

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

发布评论

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

评论(5

牵你手 2024-08-01 19:44:53

在这两种情况下,您都无需执行任何操作来取消单击链接的默认功能。

在纯 HTML 示例中,后面是指向页面顶部的链接。

您没有指定第二个示例的 href 属性是什么样子,但无论它是什么,都会遵循它。

请参阅http://icant.co.uk/articles/pragmatic-progressive-enhancement/ 对事件取消和良好的事件设计有很好的解释。 请参阅 http://docs.jquery.com/Tutorials:How_jQuery_Works 了解一些 jQuery 特定指南。

In both cases, you do nothing to cancel the default function of clicking on a link.

In the plain HTML example, the link to the top of the page is followed.

You don't specify what the href attribute for the second example looks like, but whatever it is, it will be followed.

See http://icant.co.uk/articles/pragmatic-progressive-enhancement/ for a good explanation of event cancelling and good event design. See http://docs.jquery.com/Tutorials:How_jQuery_Works for some jQuery specific guidance.

但可醉心 2024-08-01 19:44:53

改为

$('.spa_id_href').click(function(){...

and

$('.spa_id_href').click(function(evt){...//notice the evt param

函数中,调用

evt.preventDefault();

Change

$('.spa_id_href').click(function(){...

to

$('.spa_id_href').click(function(evt){...//notice the evt param

and in the function, call

evt.preventDefault();
谁的新欢旧爱 2024-08-01 19:44:53

看起来您只是按照链接目标 URL 进行操作。 这是因为您不会阻止默认的单击操作:

e.preventDefault(); // `e` is the object passed to the event handler
// or
return false

或者,您可以设置以 # 开头的 href,或者根本不使用 元素(使用 相反)——当然,如果它不是真正的链接。

It seems that you just follow the link target URL. That is because you do not prevent the default click action:

e.preventDefault(); // `e` is the object passed to the event handler
// or
return false

Alternatively, you can set up a href starting with #, or not use <a> element at all (use <span style="cursor:pointer"> instead) — if it’s not a real link of course.

何其悲哀 2024-08-01 19:44:53

这基本上与事件取消有关......
尝试这个:

try { (
   e || window.event).preventDefault(); 
} 
 catch( ex ) 
{ 
 /* do Nothing */ 
}

It's basically related to event cancelling...
Try this:

try { (
   e || window.event).preventDefault(); 
} 
 catch( ex ) 
{ 
 /* do Nothing */ 
}
陌伤ぢ 2024-08-01 19:44:53

虽然这里的其他答案对取消事件提出了很好的观点,但如果您的 JavaScript 包含阻止事件取消代码运行的错误,您仍然会遇到问题。 (如果您调试代码,可能会出现这种情况。

作为额外的预防措施,我强烈建议您不要使用href="#" 在仅触发脚本的链接上,应使用 void 运算符:

<a href="javascript:void(0)" onclick="...">...</a>

这样做的原因是:当事件未取消时,浏览器将尝试加载 < 提供的 URL。 code>href 属性。javascript:“协议”告诉浏览器改为使用随附代码的值除非该值未定义< /strong>. void 运算符明确存在以返回未定义,因此浏览器保留在现有页面上 - 无需重新加载/刷新 - 允许您继续调试脚本

这也允许您跳过整个事件 -取消仅 JS 链接的混乱(尽管您仍然需要取消附加到具有非 JS 客户端“后备”URL 的链接的代码中的事件)。

While the other answers here make excellent points about canceling events, you will still run into problems if your JavaScript contains errors which prevent your event-canceling code from being run. (As might be the case if you're, say, debugging your code.

As an additional precaution, I strongly recommend you not use href="#" on links which only trigger scripts. Instead, use the void operator:

<a href="javascript:void(0)" onclick="...">...</a>

The reason for this is: when the event is not canceled, the browser will attempt to load the URL supplied by the href attribute. The javascript: "protocol" tells the browser to instead use the value of the accompanying code unless that value is undefined. The void operator exists explicitly to return undefined, so the browser stays on the existing page — with no reload/refresh — allowing you to continue debugging your script.

This also allows you to skip the entire event-canceling mess for JS-only links (though you will still need to cancel events in code attached to links which have a "fallback" URL for non-JS clients).

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