简体   繁体   English

在我的JavaScript中手动触发href或onClick

[英]Manually trigger href or onClick in my javascript

I'm customizing a website that was build by someone else using a WYSIWYG. 我正在自定义由其他人使用所见即所得构建的网站。 I'm stuck with an href having javascript in it. 我陷入其中包含JavaScript的href。

How can I, onClick of this anchor tag, capture the href, change it's value to # , preventDefault, and then actually run what was going to originally be ran? 我如何锚标签的onClick捕获href,将其值更改为# ,preventDefault,然后实际运行最初要运行的内容? I know this is a bit odd, but it's currently causing IE errors and I believe it will fix it. 我知道这有点奇怪,但是目前正在引起IE错误,并且我相信它将解决此问题。

HTML: HTML:

<a class="cpMonthNavigation" href="javascript:CP_refreshCalendar(3,1,2012);">&lt;&lt;</a>

jQuery: jQuery的:

$(document).click({namespace: this}, function (e) {
    var t = e.data.namespace;

    if ($(e.target).is($(".cpMonthNavigation"))) {
        e.preventDefault();

        $(e.target).attr("href").replace(/javascript:/i, "");
        // This is where I need to manually run the function that was in the HREF
    }
}

Please keep in mind that I cannot edit the HTML. 请记住,我无法编辑HTML。 I'm stuck with it and it's terrible-ness. 我坚持下去,这很糟糕。

I need to preventDefault so that the vendor code does not run but still run what's actually in the href. 我需要阻止Default,以便供应商代码运行,但仍运行href中实际的内容。 There are multiple things behind the scenes in the vendor code bound to the click of this anchor. 在供应商代码中,幕后有许多事情与该锚点的绑定有关。

Try this piece of jQuery code: 试试这段jQuery代码:

$(".cpMonthNavigation").click(function (e) {
    e.preventDefault();
    var expression = $(this).attr("href"); 
    eval(expression);
    $(this).attr("href", "#");
};

I may be making this simpler than needed but to simply run the function, preventing defaults: 我可能使此过程比需要的简单,但只是运行该函数,防止出现默认值:

$(document).on('click', 'a.cpMonthNavigation', function(e) {
    e.preventDefault();
    var myHref = $(this).attr("href");
    myHref = myHref.replace(/javascript:/i, "");
    var myNewFunc = myHref.replace(/javascript:/i, /""/i);
    eval(myNewFunc);
});

a working example here: http://jsfiddle.net/MarkSchultheiss/Vta56/ 这里是一个工作示例: http : //jsfiddle.net/MarkSchultheiss/Vta56/

EDIT: shorter version of above: 编辑:以上的简短版本:

$(document).on('click', 'a.cpMonthNavigation', function(e) {
    e.preventDefault();
    eval($(this).attr("href").replace(/javascript:/i, ""));
});

You can use eval to invoke the function. 您可以使用eval调用该函数。 But that is considered as a bad practice in Javascript Programming . 但这被认为是Javascript Programming的不良做法。

The other way i could think of is, define the function inside an object like, 我可以想到的另一种方法是,在对象内部定义函数,例如

var operations = {
    CP_refreshCalendar : function(mm, dd, yyyy){
                              //your code
    }
}

Then, from the click event handler, you could do 然后,从click事件处理程序中,您可以执行

$(document).click({namespace: this}, function (e) {
var t = e.data.namespace;

if ($(e.target).is($(".cpMonthNavigation"))) {
    e.preventDefault();

    var hrefVal = $(e.target).attr("href").replace(/javascript:/i, "");
    var funcName = hrefVal.substring(0, hrefVal.indexOf('(') - 1);
    var args = hrefVal.substring(hrefVal.indexOf('('), hrefVal.index(')') - 1);
    args = args.split(',');
    if(funcName in operations){
         operations[funcName].apply(window, args);
    }

}
}

May be i am making this complex. 可能是我使这个复杂。 But i will not use eval . 但是我不会使用eval

I Hope this helps. 我希望这有帮助。

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

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