我有一個圖像,其中包含data-keyword
屬性。任何人都可以告訴我如何獲取它的字符串,並在單擊圖像時在輸入字段中顯示它?我可以使文字鏈接.special_field_link
工作,但不是圖像.image_keyword
。 (FIDDLE)下面的代碼不起作用:從點擊圖像的數據屬性到輸入框點擊
HTML
<input id="a_input_id" type="text">
<a href="" class="special_field_link">@ABC<a>
<a href="" class="special_field_link">@DEF<a>
<img src="image.jpg" class="image_keyword" data-keyword="Hello" alt="pic">
JS:
$('.special_field_link,.image_keyword').click(function (e) {
e.preventDefault();
if($(this).is('.image_keyword')){
var keyword = $(this).data('keyword');
$('#a_input_id').val( ($('#a_input_id').val()+" "+$(this).keyword.html()).trim() );
}
else {
$('#a_input_id').val( ($('#a_input_id').val()+" "+$(this).html()).trim() );
}
});
http://fiddle.jshell.net/M57vg/ – Satpal