I must use jQuery for first time....
我必须第一次使用jQuery ....
<a class="Tag Resource" href="http://localhost/"
resource="http://localres/" property="prop">test</a>
I've tried to extract the text using var = $('a').find('Tag Resource').text();
and var = $('a').find('Tag Resource').html();
but it doesn't work. I need "test" as plain text.
我试图使用var = $('a')提取文本.find('Tag Resource')。text();和var = $('a')。find('标签资源')。html();但它不起作用。我需要“测试”作为纯文本。
Can someone tell me how to do this?
谁能告诉我怎么做?
Thanks in advance
提前致谢
7 个解决方案
#1
7
I think you're looking for:
我想你正在寻找:
var t = $("a.Tag.Resource").text();
meaning a tags that have both the Tag
and Resource
classes. The find() method is for searching subtrees of elements.
表示同时具有Tag和Resource类的标记。 find()方法用于搜索元素的子树。
#2
5
Here you go (live demo):
你去(现场演示):
$(document).ready(
function (){
alert( $('a.Tag.Resource').html() );
});
Your issue is either that you wanted one class but used a space so it became two; or that when referring to classes with a jquery selector, you need to prefix them with a period.
你的问题要么你想要一个班级但是使用了一个空间,所以它变成了两个;或者在使用jquery选择器引用类时,需要在它们前面添加句点。
In any case, the above code will help. If you really just wanted one class, change it to $('a.Tag-Resource')...
无论如何,上面的代码会有所帮助。如果你真的只想要一个课程,请将其改为$('a.Tag-Resource')...
#3
1
I think the problem is the syntax of your find expression.
我认为问题是你的find表达式的语法。
Update: In fact, you don't want find at all, you want filter. Find will only select descendants of the a elements, rather than the elements themselves.
更新:事实上,你根本不想查找,你想要过滤器。 Find只会选择a元素的后代,而不是元素本身。
I've tested the example line below.
我已经测试了下面的示例行。
From the example here, it looks like you want
从这里的例子看,它看起来像你想要的
var text = $('a').filter('.Tag.Resource').text();
#4
0
var text = "";
$("a").each(function(){
text += $(this).html() + " " + $(this).attr("resource");
});
alert(text);
#5
0
I don't think you can have class names with spaces in. You've added 2 classes "Tag" and"Resource" to the a tag and your find selectory won't find that.
我不认为你可以使用带有空格的类名。你已经在a标签中添加了2个“Tag”和“Resource”类,你的find选择将找不到。
#6
0
Remember, class names can be repeated on a page, and spaces indicate two classes applied to an element. You are not guaranteed that a single element will have that class, so .text() may return the combined text of all matched elements.
请记住,类名可以在页面上重复,空格表示应用于元素的两个类。您不能保证单个元素将具有该类,因此.text()可以返回所有匹配元素的组合文本。
$(".Tag.Resource").text();
#7
0
Well, you don't have to use JQuery...
好吧,你不必使用JQuery ......
var text, links = document.links;
for (var i = 0; i < links.length; i++) {
if (links[i].className == 'Tag Resource') {
text = links[i].innerText;
break;
}
}
alert(text);
#1
7
I think you're looking for:
我想你正在寻找:
var t = $("a.Tag.Resource").text();
meaning a tags that have both the Tag
and Resource
classes. The find() method is for searching subtrees of elements.
表示同时具有Tag和Resource类的标记。 find()方法用于搜索元素的子树。
#2
5
Here you go (live demo):
你去(现场演示):
$(document).ready(
function (){
alert( $('a.Tag.Resource').html() );
});
Your issue is either that you wanted one class but used a space so it became two; or that when referring to classes with a jquery selector, you need to prefix them with a period.
你的问题要么你想要一个班级但是使用了一个空间,所以它变成了两个;或者在使用jquery选择器引用类时,需要在它们前面添加句点。
In any case, the above code will help. If you really just wanted one class, change it to $('a.Tag-Resource')...
无论如何,上面的代码会有所帮助。如果你真的只想要一个课程,请将其改为$('a.Tag-Resource')...
#3
1
I think the problem is the syntax of your find expression.
我认为问题是你的find表达式的语法。
Update: In fact, you don't want find at all, you want filter. Find will only select descendants of the a elements, rather than the elements themselves.
更新:事实上,你根本不想查找,你想要过滤器。 Find只会选择a元素的后代,而不是元素本身。
I've tested the example line below.
我已经测试了下面的示例行。
From the example here, it looks like you want
从这里的例子看,它看起来像你想要的
var text = $('a').filter('.Tag.Resource').text();
#4
0
var text = "";
$("a").each(function(){
text += $(this).html() + " " + $(this).attr("resource");
});
alert(text);
#5
0
I don't think you can have class names with spaces in. You've added 2 classes "Tag" and"Resource" to the a tag and your find selectory won't find that.
我不认为你可以使用带有空格的类名。你已经在a标签中添加了2个“Tag”和“Resource”类,你的find选择将找不到。
#6
0
Remember, class names can be repeated on a page, and spaces indicate two classes applied to an element. You are not guaranteed that a single element will have that class, so .text() may return the combined text of all matched elements.
请记住,类名可以在页面上重复,空格表示应用于元素的两个类。您不能保证单个元素将具有该类,因此.text()可以返回所有匹配元素的组合文本。
$(".Tag.Resource").text();
#7
0
Well, you don't have to use JQuery...
好吧,你不必使用JQuery ......
var text, links = document.links;
for (var i = 0; i < links.length; i++) {
if (links[i].className == 'Tag Resource') {
text = links[i].innerText;
break;
}
}
alert(text);