我不知道如果我输入输入show的值的任何字符,为什么此代码不起作用
<div>
<input class="form-control live" name="name" placeholder="Item Name"
data-class="live-title" required/>
</div>
$('.live').keyup(function () {
$($(this).data('class')).text($(this).val());
});
<div class="card cats">
<span class='price-tag'>$<span class="live-price">0</span></span>
<img class='' src='' alt=''/>
<div class='card-body'>
<h5 class='live-title'>title</h5>
<p class='live-desc'>Description</p>
<p class='live-country'>Country</p>
</div>
</div>
答案 0 :(得分:2)
$(this).data('class')
将是live-title
。因此,要选择具有此类的dom,您需要使用点(.
)为其添加类选择器前缀
$('.live').keyup(function() {
$("." + $(this).data('class')).text($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input class="form-control live" name="name" placeholder="Item Name" data-class="live-title" required/>
</div>
<div class="card cats">
<span class='price-tag'>$<span class="live-price">0</span></span>
<img class='' src='' alt='' />
<div class='card-body'>
<h5 class='live-title'>title</h5>
<p class='live-desc'>Description</p>
<p class='live-country'>Country</p>
</div>
</div>