【问题标题】:Location.href called twiceLocation.href 调用了两次
【发布时间】:2015-03-09 03:36:09
【问题描述】:

我正在做一个 Drupal 项目。我需要在按钮单击时导航到不同的页面。我在 HTML 文件中有以下按钮元素。

<a href = # id="createbutton" style=margin-left:40px;font-size:80%;>Create New Path</a></h1>

以及对应的处理程序:

$("#createbutton")
.button()
.click(function( event ) {
    event.preventDefault();
    location.href = 'http://localhost/drupal/?q=admin/waldendrupal/infograph';
});

每当我单击按钮时,页面都会加载两次。要加载的新页面中的 JavaScript 运行了两次,这是有问题的。

谁能告诉我需要做什么来纠正这个问题,以便页面只加载一次。

【问题讨论】:

  • 始终用引号将属性值括起来。
  • 当它很明显是一个链接时,你为什么称它为一个按钮,为什么不简单地将href放入链接中?
  • 谢谢。将链接位置添加到 href 属性而不是“#”会有所帮助。

标签: javascript html drupal-7


【解决方案1】:

代码应该是这样的:

<a href="#" id="createbutton" style=margin-left:40px;font-size:80%;>Create New Path</a></h1>

$("#createbutton").click(function( event ) {
    event.preventDefault();
    location.href = 'http://localhost/drupal/?q=admin/waldendrupal/infograph';
});

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 2017-06-25
    • 2013-01-21
    • 2012-09-08
    • 2016-04-27
    • 2019-02-27
    • 1970-01-01
    • 2019-12-04
    • 2018-09-15
    相关资源
    最近更新 更多