通过使用附加功能,我得到了重复的价值。下面是我的代码
$(function () {
$('.gallery_cont1').on('click',function(){
$('#template-sel').append($(this).find('h2').text()+'<br />');
});
});
对于第一次单击(意味着选择),文本正在正确添加,但对于第二次单击(意味着取消选择),之前附加的文本不会被删除。
有谁知道如何解决这个问题?
只需将append()
功能替换为html()
这样
$(function () {
$('.gallery_cont1').on('click',function(){
$('#template-sel').html($(this).find('h2').text()+'<br />');
});
});
这将检查 template-sel 元素的 HTML 是否已经等于 H2 中的文本,因此当再次单击它时,它应该删除文本。
$(function () {
$('.gallery_cont1').on('click',function(){
if($('#template-sel').html()==$(this).find('h2').text()+'<br />')
$('#template-sel').html('');
else
$('#template-sel').html($(this).find('h2').text()+'<br />');
});
});