Quick jQuery selector question :
快速jQuery选择器问题:
I need to get the content of the first element (div
/span
/...) having a class
containing a string (eg : "price").
我需要获得第一个元素(div/span/…)的内容,该类包含一个字符串(例如:“price”)。
Something not like this ;o) :
不像这样的东西;
var price = ( $('[class*=price]').text() );
Thanks a lot !!!
非常感谢! ! !
4 个解决方案
#1
4
This should work fine using the first selector:
使用第一个选择器可以很好地工作:
$('[class*="price"]:first').text()
http://jsfiddle.net/Et3vx/1
#2
1
$('[class*=price]:eq(0)').text()
would give you the required o/p.
$(' class*=price]:eq(0) .text()会给你所需的o/p。
#3
1
Try this -
试试这个,
$('[class*="price"]').eq(0).text()
Demo - http://jsbin.com/ogonap/edit#javascript,html
演示——http://jsbin.com/ogonap/edit javascript、html
#4
0
$(":contains('price')").text();
I'd imagine that should work. Haven't managed to test it.
我想这应该行得通。还没试过。
Documentation for :contains is here http://api.jquery.com/contains-selector/#text Basically finds the element containing your string and returns the class.
包含的文档:http://api.jquery.com/contains-selector/#text找到包含您的字符串并返回类的元素。
EDIT: My bad, I thought you meant search for the string 'price' and return all text.
编辑:我的错,我以为你的意思是搜索字符串“price”并返回所有文本。
#1
4
This should work fine using the first selector:
使用第一个选择器可以很好地工作:
$('[class*="price"]:first').text()
http://jsfiddle.net/Et3vx/1
#2
1
$('[class*=price]:eq(0)').text()
would give you the required o/p.
$(' class*=price]:eq(0) .text()会给你所需的o/p。
#3
1
Try this -
试试这个,
$('[class*="price"]').eq(0).text()
Demo - http://jsbin.com/ogonap/edit#javascript,html
演示——http://jsbin.com/ogonap/edit javascript、html
#4
0
$(":contains('price')").text();
I'd imagine that should work. Haven't managed to test it.
我想这应该行得通。还没试过。
Documentation for :contains is here http://api.jquery.com/contains-selector/#text Basically finds the element containing your string and returns the class.
包含的文档:http://api.jquery.com/contains-selector/#text找到包含您的字符串并返回类的元素。
EDIT: My bad, I thought you meant search for the string 'price' and return all text.
编辑:我的错,我以为你的意思是搜索字符串“price”并返回所有文本。