简体   繁体   中英

Javascript - onClick return what was clicked on

so, I have a div with stuff like <h2> s and <img> s in them, and I want it so that if a user doubleclicks on one of them (for example they double click on anything inside the <h2></h2> tags it will turn it into a textbox with <h2>title</h2> inside it...

I just can't figure out how to send to the javascript function what was double clicked on?

try if you are famous with jQuery:

$('#yourDiv *').dblclick(function(event)){
 alert(event.target.html());
}

Here is a very bare example for single click :

<script type="text/javascript">
  function foo(elem) {
      elem.innerHTML = '<input type="text" value="' + elem.innerHTML + '">';
  }
</script>
<h1 onClick="foo(this)">Hello</h1>
<h1 onClick="foo(this)">My</h1>
<h1 onClick="foo(this)">Friend</h1>

test live: http://jsfiddle.net/MsTzY/

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