开发者

Click() method is not working on HyperLink object

I am trying to call click() method dynamically on an object of a hyper link through java script code.Getting error that click() is not a method.

Can someone please let me know how can I do this.I tried to wrap this object around Mootools and JQuery object.Still did not get through.

Here is some code demonstration

<a href="abc.jsp" id="myLink1">Click here</a>
<a href="abcd.jsp" id="myLink2开发者_如何学运维">Click here</a>

<script>
jQuery(document).ready(){
        $('myLink1').click(function() {
       //doing some reporting stuff
        });

       $('myLink2').click(function() {
       //doing some reporting stuff
       });
  }

//I need some way ,click event to be fired on links depending on some conditions.

function clickDynamically()
{
  if(clickFirstLink)
         document.getElementById('myLink1').click(); // does not work , it says click is not a method
  else
    document.getElementById('myLink2').click();
}
</script>


If you're using jQuery, you would do this:

$("#mylink").click();

If you're not using jQuery, you would do this:

document.getElementById('mylink').click();

Note, you'll need to make sure you set up the event handler first. In jQuery, you'll do it like this:

$("#mylink").click(function() { alert("works"); });

EDIT

In response to your comment, here is a working example that you can build off of:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Page Title</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

    <script type="text/javascript" charset="utf-8">

        $(function() {

            // Example #1
            // Set up different click handlers on each link
            // Be sure to return false so the browser DOESN'T
            // follow the links.

            $("#link-1").click(function() {

                alert("Link #1");

                return false;

            });

            $("#link-2").click(function() {

                alert("Link #2");

                return false;

            });

            //////// OR /////////

            // Example #2
            // Set up 1 click handler and use logic to determine
            // which link was clicked.
            // Be sure to return false so the browser DOESN'T
            // follow the links.

            $("#link-1, #link-2").click(function() {

                var $LinkThatWasClicked = $(this);

                if ($LinkThatWasClicked.attr("id") == "link-1") {

                    // Do something if it's link 1
                    alert("We figured out it is link #1");

                } else {

                    // Do something if it's link 2
                    alert("We figured out it is link #2");

                }

                return false;

            });

        });

    </script>

</head>
<body>

    <a href="#" id="link-1">Click Link #1</a>
    <a href="#" id="link-2">Click Link #2</a>

</body>
</html>

I think the issue with the jQuery code you posted was simply that you forgot the hash symbol before myLink. The has tells jQuery to look for an element with an ID of myLink. Without the hash, jQuery was looking for an element with a tagName of myLink, which is invalid.

Hope the example helps.


You can do:

window.location = document.getElementById("myLink").href;


  jQuery(document).ready(){
        $('yourVar').click(function() {
       //your code
    });
  }

it will work in any case :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜