简体   繁体   English

使用jquery提取href标签的值

[英]extract value of href a tag using jquery

<tr class="field_20 field_facebook">
<td class="label">Facebook</td>
<td class="data"><p><a href="http://facebook.com/facebook">Test</a></p>
</td></tr>
<img id="fb" src="fb.png">

Hello, so this is what I have and I want to select using jquery the value http://facebook.com/facebook and open the link. 您好,这就是我所拥有的,我想使用jquery选择值http://facebook.com/facebook并打开链接。 this is what I have so far: 这是我到目前为止所拥有的:

<script>
jQuery(document).ready(function(){
jQuery("#fb").click(function() {
  var fb = jQuery(".field_20 field_facebook").find("a").attr("href");
window.open(fb);

}); //click function ends
}); // document ready ends
</script>

any help would be greatly appreciated thank you. 任何帮助将不胜感激,谢谢。

Use .field_20.field_facebook (no space, but a period). 使用.field_20.field_facebook (没有空格,而是一个句点)。

The class attribute allows you to define multiple classes on the same element with a space. class属性允许您在同一元素上用空格定义多个类。 This is distinct from jQuery (and CSS) selectors where the space is a descendant selector. 这不同于jQuery(和CSS)选择器,后者的空间是后代选择器。 That is, .field_20 field_facebook tries to select a <field_facebook> element that is a descendant of an element with the .field_20 class. 也就是说, .field_20 field_facebook尝试选择<field_facebook>元素,该元素是.field_20类的元素的后代。 Using .field_20 .field_facebook would not work either because the descendant selector is still there. 使用.field_20 .field_facebook也不起作用,因为后代选择器仍然存在。 Omitting the space is proper syntax for selecting via multiple classes on the same element. 省略空间是通过同一元素上的多个类进行选择的正确语法。

Try this as your jQuery script: 尝试将此作为您的jQuery脚本:

$(document).ready(function() {

    $("#fb").click(function() {
        var fb = $('td.data a').attr('href');
        alert(fb);
        window.open(fb);
    }); //click function ends

}); //END $(document).ready()

Your selector line can be simplified to: 您的选择器行可以简化为:

var fb = jQuery(".field_20.field_facebook a").attr("href");

Just like in CSS, you can select the a which is a child of an element with class .field_20.field_facebook . 就像在CSS,你可以选择a它是一个类元素的子.field_20.field_facebook

<tr class="field_20 field_facebook">

The classes are available on the same element. 这些类在同一元素上可用。

So there should be no space between the 2 classes when used in the selector. 因此,在选择器中使用时,两个类之间不应有空格。

jQuery(".field_20.field_facebook")

Use the line below to access the facebook link 使用下面的行访问facebook链接

jQuery(".field_20.field_facebook a").attr("href");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM