开发者

jquery atrr("href") is not consistent ..in sharepoint

I have a jquery function(This is not written by me anyway still I am learning). In that we are replacing urls using f.attr("href") in several places. I am not understanding that from where this href value will be binded. And why the value(f.attr("href")) is changing place to place. I mean to say it is having some value @ one location and if I give the same it is giving me different value at other location.

I read in article that The .attr() method gets the attribute value for only the first element in the matched set..What is the matched set means

2

Let me modify my question like this: Can I append query string in Jquery function? I want to add two more querystring elements from webpage to get f开发者_C百科ull url. If we can..How can we accomplish that?


.replace("${my_email}", "mailto:my@u.com?subject=Thanku &body=Jquery working.%0A%0ahttp://www.my.site.com/_layouts/products/New.aspx?url=" + f.attr("href")) In this the f.attr("href") is giving me different value than others where we used..Basically it is giving me half of the link only..

Is there any thing that you need I can provide you Thank u


When you use a selector in a jQuery call it may match more than one element. The results are returned as a jQuery object, which may contain a collection (array) of matched objects. Some methods, like attr only work on the first element in the collection.

For example,

$('a')

will match all anchor tags in your document.

Now each of these will (may) have an href attribute, but if you apply the attr function to the results of that selector, it will only operate on the first element in the collection. If I have the following HTML.

 <a href='#0'>Zero</a>
 <a href='#1'>One</a>

and use

var href = $('a').attr('href');

Then the href variable will have #0 as it's value despite the fact that the selector matches both of the anchors.

Making it somewhat more confusing is that usually the version of the method that assigns values, rather than read values, will work on all of the elements in the collection. For example,

$('a').attr('title','foo');

will assign foo to the title attribute of both anchors in the collection. It's important to understand the differences in the way the various methods operate. See the jQuery API for more details on specific methods.

In your case, I suspect that f is a variable that matches different selectors in different locations in the code (f is a lousy variable name, btw, making it more verbose would probably help in understanding what the code does). That would explain why it has different values for the href attribute in different locations. Of course, the values could also be changing precisely because you're setting them, but I would hope you would have understood if that were happening.


If you have several anchor tags on the page, you stand the chance of selecting more than one of them.

<a href="#">Missed</a>
<div>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <span><a href="#">Missed</a></span>
</div>

If you did $('div > a') as your jQuery selector, you would match (in this example) all anchor tags whose contents is "Anchor"; because this selector only matches anchor tags who are children of divs.

You can see how many elements are your matched set by calling alert(f.length);

If you can provide the code that defines f, or see some of the HTML in your page, we'll be able to help you more specifically!

If you set a value using f.attr(), all matched elements will update. If you get a value using f.attr(), only the value for the first element will be returned.


A matched set is zero and more elements which are the result of the initial query, for example $('.foo') will return a set with all elements which has the class foo defined. When you call .attr('href') on that set it will only return the attribute for the first element in the set, if any, as returning all attributes in an array or concatenates isn't what most people want usually.

If you really want all attributes, you can do as following:

var array_of_hrefs = $('.foo').each(function(){
  return $(this).attr('href')
}).get();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜