开发者

Javascript / Jquery, open a link with a REL tag as if it were clicked?

The designer that I am working with on a project implemented a pop-up so that i开发者_运维百科t is called when a static link is clicked like

<a href="#" rel="#dialog">Testing Dialog</a>

Of course, I don't want it as a static link as I have to do things to it before it is shown to the user so I am wondering if anyone knows of a way to make a call with Javascript to do the same thing as if the link above were clicked by the user? Any advice is greatly appreciated


If I understand correctly, with jQuery:

$("a[rel='dialog']").click();

or:

$("a[rel='dialog']").trigger("click");

Demo: http://jsfiddle.net/karim79/fc6Yk/


Basic javascript for triggering a click on an element:

var clicky = document.createEvent("HTMLEvents"); 
clicky.initEvent("click", true, true); 
targetElement.dispatchEvent(clicky); 

Docs!

  • https://developer.mozilla.org/en/DOM/document.createEvent
  • https://developer.mozilla.org/en/DOM/event.initEvent
  • https://developer.mozilla.org/en/DOM/element.dispatchEvent

For more detailed click events, see: https://developer.mozilla.org/en/DOM/event.initMouseEvent


div
for js:

function popitup(url) {
        newwindow=window.open(url,'name','height=200,width=150');
        if (window.focus) {newwindow.focus()}
        return false;
    }

for html:

<div onclick="popitup()"> When you click this, it will pop up</diva>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜