如何在jquery中基于css类获取span标签的值?

时间:2022-01-21 20:28:57

How to get the value of span tag based on css class in jquery?

如何在jquery中基于css类获取span标签的值?

<span class="page-numbers current">3</span>

I used

var pageno = $(".page-numbers current").val();
  alert(pageno);

it doesnt seem to work.any suggestion..

它似乎没有工作。任何建议..

EDIT:i am using jquery pagination plugin for pagination... for first page 1 and prev are assigned css class class="page-numbers current

编辑:我正在使用jquery分页插件进行分页...对于第一页和上一页分配了css class class =“page-numbers current

<span class="page-numbers current prev">Prev</span>
<span class="page-numbers current">1</span>

3 个解决方案

#1


1  

".page-numbers" and "current" are 2 different css classes.

“.page-numbers”和“current”是两个不同的css类。

Your jQuery expects "current" to be inside of ".page-numbers".

你的jQuery期望“当前”在“.page-numbers”里面。

Also use text() for non-input elements.

也可以将text()用于非输入元素。

This should work

这应该工作

alert($(".page-numbers").text());

EDIT: Furthermore, current hasn't got a class (.) or id (#) prefix which means it's looking for an element "current", which doesn't exist. Use #current or .current

编辑:此外,当前没有类(。)或id(#)前缀,这意味着它正在寻找一个不存在的元素“当前”。使用#current或.current

#2


1  

var pageno = $(".page-numbers.current").text();
  alert(pageno);

see second example class-selector

看第二个示例类选择器

#3


0  

I assume only the current page was the class .current so grab it using only that selector.

我假设只有当前页面是类.current所以只使用该选择器抓取它。

Grab it using jQuery .html() like this:

使用jQuery .html()抓住它,如下所示:

// do notice that a . was added to class .current!
var pageno = $(".current").html();
alert(pageno);


You were trying to capture the val that exists in fields like input.

您试图捕获输入中的字段中存在的val。

// on a field like this what you were doing would work perfectly
<input type='text' val='12' />

#1


1  

".page-numbers" and "current" are 2 different css classes.

“.page-numbers”和“current”是两个不同的css类。

Your jQuery expects "current" to be inside of ".page-numbers".

你的jQuery期望“当前”在“.page-numbers”里面。

Also use text() for non-input elements.

也可以将text()用于非输入元素。

This should work

这应该工作

alert($(".page-numbers").text());

EDIT: Furthermore, current hasn't got a class (.) or id (#) prefix which means it's looking for an element "current", which doesn't exist. Use #current or .current

编辑:此外,当前没有类(。)或id(#)前缀,这意味着它正在寻找一个不存在的元素“当前”。使用#current或.current

#2


1  

var pageno = $(".page-numbers.current").text();
  alert(pageno);

see second example class-selector

看第二个示例类选择器

#3


0  

I assume only the current page was the class .current so grab it using only that selector.

我假设只有当前页面是类.current所以只使用该选择器抓取它。

Grab it using jQuery .html() like this:

使用jQuery .html()抓住它,如下所示:

// do notice that a . was added to class .current!
var pageno = $(".current").html();
alert(pageno);


You were trying to capture the val that exists in fields like input.

您试图捕获输入中的字段中存在的val。

// on a field like this what you were doing would work perfectly
<input type='text' val='12' />