$(this).val()无法使用jQuery从跨度获取文本 [英] $(this).val() not working to get text from span using jquery
本文介绍了$(this).val()无法使用jQuery从跨度获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出此html,当我单击它时,我想从中获取八月":
Giving this html, i want to grab "August" from it when i click on it:
<span class="ui-datepicker-month">August</span>
我尝试过
$(".ui-datepicker-month").live("click", function () {
var monthname = $(this).val();
alert(monthname);
});
但似乎不起作用
推荐答案
Instead of .val()
use .text()
, like this:
$(".ui-datepicker-month").live("click", function () {
var monthname = $(this).text();
alert(monthname);
});
或者在jQuery 1.7+中使用on()
,因为不推荐使用live
:
Or in jQuery 1.7+ use on()
as live
is deprecated:
$(document).on('click', '.ui-datepicker-month', function () {
var monthname = $(this).text();
alert(monthname);
});
.val()
用于输入类型元素(包括文本区域和下拉列表),因为处理具有文本内容的元素,请在此处使用 .text()
.
这篇关于$(this).val()无法使用jQuery从跨度获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文