javascript - pass the object to function but can not recognize -
my code
<html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> function process(e) { alert("hello! alert box1!!"); e.hide(); }; $(document).ready(function() { var p = $("#id1"); //jquery object p.click(function() { process(this); //line }); }); </script> </head> <body> <p id=id1>if click on me, disappear.</p> </body> </html>
the text dies not disappear when click, console reports error:
typeerror: e.hide not function
but if change code @ line below
<html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> function process(e) { alert("hello! alert box1!!"); e.hide(); }; $(document).ready(function() { var p = $("#id1"); //jquery object p.click(function() { process(p); //change p, line }); }); </script> </head> <body> <p id=id1>if click on me, disappear.</p> </body> </html>
it works!!!
i try code:
<html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> function process(e) { alert("hello! alert box1!!"); e.style.visibility = "hidden"; }; $(document).ready(function() { var p = $("#id1")[0]; //jquery object dom object p.addeventlistener("click", function() { process(this); }); }); </script> </head> <body> <p id=id1>if click on me, disappear.</p> </body> </html>
it works also.
you comment welcome
you can see how javascript handling sending using console.log()
.
doing console.log(this)
gives in log:
<p id="id1">if click on me, disappear.</p>
which can see plain dom object.
however, doing console.log(p)
gives in log:
[p#id1, context: document, selector: "#id1"]
that represents jquery object can acted on. if want jquery pull object this
, wrap send in jquery:
function process(e){ alert("hello! alert box1!!"); $(e).hide(); // wrap convert dom object. }; $(document).ready(function(){ var p =$("#id1");//jquery object p.click(function(){ process(this);//change p, line }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id=id1>if click on me, disappear.</p>
Comments
Post a Comment