简体   繁体   English

在调用href时标识锚标记

[英]Identifying the anchor tag when href is called

How can you identify in a function whether it has been invoked by an anchor tag href? 您如何在函数中标识是否已由锚标记href调用?

event is null in this case. 在这种情况下,事件为null。 So event.target and event.srcElement won't work. 因此event.target和event.srcElement无法使用。

Code

HTML 的HTML

<a href="SomeFunction();">Href works here</a>

JavaScript 的JavaScript

   function SomeFunction ()
   {
      // I need to get the anchor element that invoked this function
   }

What about 关于什么

<a href="SomeFunction(this);">Href works here</a>

function SomeFunction(context) {

    var callingElement = context;

}

Following what @alex suggested, can you add a script to run in the page load to change the hrefs to be what you want (adding the 'this' reference)? 按照@alex的建议,您是否可以添加脚本以在页面加载中运行以将href更改为所需的内容(添加“ this”引用)?

Take the following script for example, this will change the href value for anchor tags with id set to SomeID or class set to SomeClass: 以以下脚本为例,这将更改ID设置为SomeID或类设置为SomeClass的锚标记的href值:

function changeLinks() {
    var all_links = document.getElementsByTagName("a");
    for (var i=0; i<all_links.length; i++){
        if (all_links[i].id == 'SomeID' || all_links[i].className == 'SomeClass') {
            all_links[i].href = 'SomeFunction(this);';
        }
    }
}

Hope this helps... 希望这可以帮助...

Edit : Following your comment, you can try this: 编辑 :根据您的评论,您可以尝试以下操作:

var clickedAnchor = null;

function setClickedAnchor(obj) {
    clickedAnchor = obj;
}
function changeLinks() {
    var all_links = document.getElementsByTagName("a");
    for (var i=0; i<all_links.length; i++){
        if (all_links[i].id == 'SomeID' || all_links[i].className == 'SomeClass') {
            all_links[i].href = 'setClickedAnchor(this);' + all_links[i].href;
        }
    }
}

As far as I know there are only two ways to accomplish this using standard Javascript. 据我所知,只有两种方法可以使用标准Javascript完成此操作。 The first way has already been outlined -- pass the node as a parameter to the function. 已经概述了第一种方法-将节点作为参数传递给函数。 The other way would be to handle the onclick event of the anchor. 另一种方法是处理锚点的onclick事件。 This is very similar to the previous approach in that is requires that you modify the markup to pass a parameter to a function. 这与以前的方法非常相似,因为它要求您修改标记以将参数传递给函数。 To do this you'll need to change the markup to the following: 为此,您需要将标记更改为以下内容:

<a href="#" onclick="SomeFunction(event)">Href works here</a>

function SomeFunction(event) {
    var node = event.srcElement;
}

The above code would pass the event object along to the function which would give you all sorts of interesting information about the event. 上面的代码会将事件对象传递给函数,该函数将为您提供有关事件的各种有趣信息。

If you're unable to change the markup that is sent to the browser, you might want to consider using something like JQuery or another AJAX library to search for and modify the event handlers of the nodes at runtime. 如果您无法更改发送到浏览器的标记,则可能需要考虑使用诸如JQuery或其他AJAX库之类的东西在运行时搜索和修改节点的事件处理程序。 Modifying the markup before it gets to the browser is obviously preferred, but sometimes you don't have a choice. 在将标记添加到浏览器之前对其进行修改显然是首选,但是有时您别无选择。

Lastly, if you cannot change the markup and don't want to modify the DOM at runtime, you can always see what Javascript engine specific features are available. 最后,如果您无法更改标记并且不想在运行时修改DOM,则始终可以看到哪些Java引擎特定功能可用。 For example, you might be able to make use of arguments.caller in those engines that support it. 例如,您也许可以在支持它的引擎中使用arguments.caller I'm not saying that it will work, but you might want to see what's available. 我并不是说它会起作用,但是您可能想查看可用的东西。

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

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