简体   繁体   English

Javascript-onClick返回所点击的内容

[英]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... 因此,我有一个div,其中包含诸如<h2><img>的内容,因此我希望它能够使用户双击其中的一个(例如,他们双击<h2></h2>内的任何内容<h2></h2>标签,它将变成一个带有<h2>title</h2>的文本框...

I just can't figure out how to send to the javascript function what was double clicked on? 我只是不知道如何将双击发送到javascript函数?

try if you are famous with jQuery: 尝试如果您以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/ 实时测试: http//jsfiddle.net/MsTzY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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