简体   繁体   English

JavaScript:切换链接有效/无效

[英]JavaScript: toggle links active/deactive

How do I toggle HTML links from being active / deactive using JavaScript? 如何使用JavaScript将HTML链接从活动状态切换为非活动状态?

Within my HTML, I have 2 hyper links that call a JavaScript function to sort a table: 在我的HTML中,我有2个超链接,它们调用JavaScript函数对表进行排序:

<a href="javascript:sort('asc');">ASC</a> | <a href="javascript:sort('desc');">DESC</a>

What I would like to do is when someone clicks the "ASC" link, it then deactives the "ASC" hyperlink (no longer a link) so that only now the "DESC" link is active. 我想做的是,当有人单击“ ASC”链接时,它会停用“ ASC”超链接(不再是链接),以便仅现在激活“ DESC”链接。

Then, if the person presses the "DESC" link, it then disables the "DESC" link and re-enables the "ASC" link. 然后,如果该人按下“ DESC”链接,则它将禁用“ DESC”链接并重新启用“ ASC”链接。

Essentially, I want to toggle between which link is active, either: "ASC" or "DESC" 本质上,我想在哪个链接处于活动状态之间进行切换:“ ASC”或“ DESC”

I assume you can do this with JavaScript but am not sure how? 我认为您可以使用JavaScript来做到这一点,但不确定如何?

Any ideas? 有任何想法吗?

Doesn't strictly "remove" the A tag, but removes the "linkness" (by removing the href) from the tag. 严格来说,并不是严格地“删除” A标签,而是从标签中删除“链接”(通过删除href)。

Edit: slightly better version (now tested): 编辑:稍好一点的版本(现已测试):

<script type="text/javascript" >

  function mySort( sortorder) {

      // enable the other link
      otherOrder = (sortorder == 'asc') ? 'desc' : 'asc';
      document.getElementById(otherOrder).setAttribute("href", "#");
      document.getElementById(otherOrder).onclick = function() {mySort(this.id)};

      //disable this link
      document.getElementById(sortorder).removeAttribute("href");
      document.getElementById(sortorder).onclick = "";

      //perform the sort
      doSort(sortorder);
  }
</script>

<a id="asc" href="#" onclick="mySort(this.id)" >ASC</a> | <a id="desc" href="#" onclick="mySort(this.id)" >DESC</a>

you could use: in your script section: 您可以在脚本部分使用:

var asc = true;

and your html 和你的HTML

<a href="javascript:if (asc) { sort('asc'); asc=!asc; }; ">ASC</a> | <a href="javascript:if (!asc) { sort('desc'); asc=!asc; }; ">DESC</a>

(though it might be better to spin those off into functions) (尽管最好将它们分解为功能)

EDIT 编辑

as per author's comment, if you want to completely "remove" the links, switch display:none and display:inline on the elements. 根据作者的评论,如果要完全“删除”链接,请在元素上切换display:none和display:inline。 eg 例如

<a id="asc" href="if (asc) { sort('asc'); asc=!asc; this.style.display = 'none'; document.getElementById("desc").style.display = 'inline';}; ">ASC</a> | <a id="desc" href="if (!asc) { sort('desc'); asc=!asc; this.style.display = 'none'; document.getElementById("asc").style.display = 'inline';}; ">DESC</a>

If you want to replace them with spans, then use the same technique to show/hide their span counterparts 如果要用跨距替换它们,请使用相同的技术来显示/隐藏其跨距对应项

Are you familiar with jQuery? 您熟悉jQuery吗? If so, you might consider the various table-sorting plugins out there, such as TableSorter . 如果是这样,您可以考虑使用各种表排序插件,例如TableSorter I don't have any use for it at the moment, but a number of shops in my organization have incorporated it. 我目前没有任何用途,但是我组织中的许多商店都已将其合并。 For bonus points, the documentation at that link seems pretty comprehensive. 对于奖励积分,该链接上的文档似乎非常全面。

jQuery can also give you solid control over whether the <a> is present. jQuery还可以使您对<a>是否存在进行可靠控制。 Hopefully someone with more experience can stop by w/ some code examples demonstrating that. 希望有更多经验的人可以停下一些示例代码来证明这一点。

Don't make them links, use another element, for example a span . 不要使它们链接,而要使用其他元素,例如span

<span onclick="sort('asc');">ASC</span> | <span onclick="sort('desc');">DESC</span>

Then use JavaScript to set a class that defines a visual style on the active item, you can toggle this on each span . 然后使用JavaScript设置一个在活动项目上定义视觉样式的类,您可以在每个span上切换该样式。

Your sort function can also determine whether or not the supplied direction is valid and simply do nothing. 您的排序功能还可以确定所提供的方向是否有效,并且什么也不做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM