Jquery - 单击以将父元素复制到文本字段

时间:2013-10-24 16:45:41

标签: javascript jquery html click copy

我希望每个网址旁边有4个按钮,然后您可以点击按钮将父网址复制到按钮各自的文本字段。

我认为使用jquery最简单,但我不知道从哪里开始。任何建议将不胜感激!

http://jsfiddle.net/WwdMw/

<div class="selection">
  <div class"url">
    <h2>example1.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>
<hr/>
<div class="selection">
  <div class"url">
    <h2>example3.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>
<hr/>

<div class="selection">
  <div class"url">
    <h2>example2.com</h2>
  </div>
  <button type="button" class="btn_opt1">Copy to Option1</button>
  <button type="button" class="btn_opt2">Copy to Option2</button>
  <button type="button" class="btn_opt3">Copy to Option3</button>
  <button type="button" class="btn_opt4">Copy to Option4</button>
</div>

<hr/>

1 个答案:

答案 0 :(得分:1)

拥有它。

$('button').click(function () {
    var value = $(this).parent().find('h2').text();
    var idIdentifier = $(this).text().substr($(this).text().length - 1);
    var textArea = $('div[class*='+idIdentifier+']').find('textarea');
    textArea.val(textArea.val() +" "+ value);
})

Live Demo