使用jquery从span获取文本

时间:2022-12-15 20:30:32

Giving this html, i want to grab "August" from it when i click on it:

给这个html,当我点击它时,我想从它中抓取“August”:

<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

但似乎并不奏效

7 个解决方案

#1


181  

Instead of .val() use .text(), like this:

使用.text()代替.val(),如下所示:

$(".ui-datepicker-month").live("click", function () {
    var monthname =  $(this).text();
    alert(monthname);
});

Or in jQuery 1.7+ use on() as live is deprecated:

或者,在jQuery 1.7+中使用()作为live被弃用:

$(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.

.val()表示输入类型元素(包括文本区域和下拉),因为要处理文本内容的元素,这里使用.text()。

#2


18  

I think you want .text():

我想你想要。text():

var monthname = $(this).text();

#3


6  

To retrieve text of an auto generated span value just do this :

要检索自动生成的span值的文本,只需这样做:

var al = $("#id-span-name").text();    
alert(al);

#4


4  

-None of the above consistently worked for me. So here is the solution i worked out that works consistently across all browsers as it uses basic functionality. Hope this may help others. Using jQuery 8.2

-以上这些对我都不起作用。这是我研究出来的解决方案,它在所有浏览器中使用基本功能时都是一致的。希望这能帮助到其他人。使用jQuery 8.2

1) Get the jquery object for "span". 2) Get the DOM object from above. Using jquery .get(0) 3) Using DOM's object's innerText get the text.

1)获取“span”的jquery对象。2)从上面获取DOM对象。使用jquery .get(0) 3)使用DOM对象的innerText获取文本。

Here is a simple example

这里有一个简单的例子

var curSpan = $(this).parent().children(' span').get(0);
var spansText = curSpan.innerText;

HTML

HTML

<div >
<input type='checkbox' /><span >testinput</span> 
</div>

#5


3  

You can use .html() to get content of span and or div elements.

可以使用.html()获取span和div元素的内容。

example:

例子:

    var monthname =  $(this).html();
    alert(monthname);

#6


0  

Here we go:

我们开始吧:

$(".ui-datepicker-month").click(function(){  
var  textSpan = $(this).text();
alert(textSpan);   
});

Hope it helps;)

希望它能帮助;)

#7


0  

.val() is for input elements, use .html() instead

.val()表示输入元素,使用.html()代替

#1


181  

Instead of .val() use .text(), like this:

使用.text()代替.val(),如下所示:

$(".ui-datepicker-month").live("click", function () {
    var monthname =  $(this).text();
    alert(monthname);
});

Or in jQuery 1.7+ use on() as live is deprecated:

或者,在jQuery 1.7+中使用()作为live被弃用:

$(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.

.val()表示输入类型元素(包括文本区域和下拉),因为要处理文本内容的元素,这里使用.text()。

#2


18  

I think you want .text():

我想你想要。text():

var monthname = $(this).text();

#3


6  

To retrieve text of an auto generated span value just do this :

要检索自动生成的span值的文本,只需这样做:

var al = $("#id-span-name").text();    
alert(al);

#4


4  

-None of the above consistently worked for me. So here is the solution i worked out that works consistently across all browsers as it uses basic functionality. Hope this may help others. Using jQuery 8.2

-以上这些对我都不起作用。这是我研究出来的解决方案,它在所有浏览器中使用基本功能时都是一致的。希望这能帮助到其他人。使用jQuery 8.2

1) Get the jquery object for "span". 2) Get the DOM object from above. Using jquery .get(0) 3) Using DOM's object's innerText get the text.

1)获取“span”的jquery对象。2)从上面获取DOM对象。使用jquery .get(0) 3)使用DOM对象的innerText获取文本。

Here is a simple example

这里有一个简单的例子

var curSpan = $(this).parent().children(' span').get(0);
var spansText = curSpan.innerText;

HTML

HTML

<div >
<input type='checkbox' /><span >testinput</span> 
</div>

#5


3  

You can use .html() to get content of span and or div elements.

可以使用.html()获取span和div元素的内容。

example:

例子:

    var monthname =  $(this).html();
    alert(monthname);

#6


0  

Here we go:

我们开始吧:

$(".ui-datepicker-month").click(function(){  
var  textSpan = $(this).text();
alert(textSpan);   
});

Hope it helps;)

希望它能帮助;)

#7


0  

.val() is for input elements, use .html() instead

.val()表示输入元素,使用.html()代替