简体   繁体   中英

how to add onclick event to anchor tag that is sitting within iframe?

before clicking this anchor tag I would like to do something else:

anchorref.onlick= function()
{
//do something
//progress to goal.com
return (true);

}

my html looks (something) like this, the point is having an anchor tag witin a button that you would like to do something else first before heading of to the goal.com. The Iframe is pointing to another domain. Or should I maybe create an event handler on the Iframe tag?:

<iframe class="myclass" frameborder="0" scrolling="no" allowtransparency="true" src="http://www.goal.com"
            style="width: 55px; height: 20px;">
            <html lang="en">
            <head>
                <body class="ncount">
                    <span id="tweet-button" class="tb-container"><span class="tb"><a id="btn" tabindex="1"
                        href="http://www.goal.com"</span>
                        <img src="someimagesrc" alt="" style="height: 1px; width: 1px;">

                </body>
            </html>
        </iframe>

The HTML tag is not valid inside the IFRAME tag. Place your anchor outside the IFRAME and attach your event to the anchor.

Here I bind the event in the HTML:

<html>
<head>

<script type="text/javascript">
    function SetIFrameURL() {
        alert('Do something here!?');
        document.getElementById("iframe1").src = "http://www.goal.com";
    } 
</script>

</head>
<body>

<a href="javascript:SetIFrameURL()">HIT ME!</a><br />

<iframe id="iframe1" class="myclass" frameborder="0" scrolling="no" allowtransparency="true"
                style="width: 550px; height: 200px;">
            </iframe>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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