使用jquery从span获取文本 [英] Get text from span using jquery
本文介绍了使用jquery从span获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< span class 给这个html,我想从它抓取August = UI-日期选择器月 > 8月< /跨度>
我试过了
< code $(。ui-datepicker-month)。live(click,function(){
var monthname = $(this).val();
alert(monthname );
});
但似乎不工作
解决方案
而不是 .val() code>
使用 .text()
,像这样:
$(。ui-datepicker-month)。live(click ,函数(){
var monthname = $(this).text();
alert(monthname);
});
或者在jQuery 1.7+中使用 on()
作为 live
已弃用:
$(document).on( ();
alert(monthname);
});
.val()
适用于输入类型元素(包括textareas和dropdowns),因为您正在处理包含文本内容的元素,请使用< a href =http://api.jquery.com/text/ =noreferrer> .text()
这里。
Giving this html, i want to grab "August" from it when i click on it:
<span class="ui-datepicker-month">August</span>
i tried
$(".ui-datepicker-month").live("click", function () {
var monthname = $(this).val();
alert(monthname);
});
but doesn't seem to be working
解决方案
Instead of .val()
use .text()
, like this:
$(".ui-datepicker-month").live("click", function () {
var monthname = $(this).text();
alert(monthname);
});
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()
is for input type elements (including textareas and dropdowns), since you're dealing with an element with text content, use .text()
here.
这篇关于使用jquery从span获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文