Jquery - starting with the element passed to the function using this pointer

I'm very new to jQuery, so this can be a really dumb question. :-) I'm trying to get something like the following:

<html>
<head>
    <title>JQuery test</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("jquery", "1"); // Load jQuery

      function DoSomething(link)
      {
        $(link).after('<div>hello world!</div>');
      }
    </script>
</head>
<body>
    <a href="javascript: DoSomething(this);">Click!</a>
</body>
</html>

Now, it seems, does nothing. But if I changed the line to:

$('a').after('<div>hello world!</div>');

Then a div is added. However, I want to use the element passed to the function to control where the element is inserted. Basically, I can't figure out how to start a jQuery query based on an element passed to a function using the "this" pointer.

Thanks for the help,

~ Justin

+3
source share
2 answers

jQuery javascript HTML, : href="javascript: ..." href="#" onclick="...". - . - , , .

<a href="#" id="myLink">Click!</a>

jQuery:

$('#myLink').click(function() {
    $(this).after("<div>hello world!</div>");
});

, this DOM, .

jQuery - :

$(domElement)

- , . :

<a href="#" onclick="doSomething(this)">

doSomething() return false

+3

<a href="javascript: DoSomething(this);">Click!</a> <a href="#" onclick="doSomething(this)">.

this JavaScript. , onclick, .

javascript: -URL- . . . , -. , this window, , , this.

javascript: -URL- (, ). onclick , return false;, # .

( onclick, , , , - DoSomething.)

+1

Source: https://habr.com/ru/post/1719843/


All Articles