admin管理员组

文章数量:1431740

I'm trying to set the base href with JQuery by reading a PHP file.

Why does $("base").load("ip.php"); work but $("base[property='href']").load("ip.php"); doesn't?

Of course what I need is to set the href parameter only, not the content of base. I found I could get it working by using $("head").load("ip.php"); but would like to know why above isn't working.

I'm trying to set the base href with JQuery by reading a PHP file.

Why does $("base").load("ip.php"); work but $("base[property='href']").load("ip.php"); doesn't?

Of course what I need is to set the href parameter only, not the content of base. I found I could get it working by using $("head").load("ip.php"); but would like to know why above isn't working.

Share Improve this question edited Mar 12, 2016 at 19:13 Alexis Tyler 9686 gold badges32 silver badges51 bronze badges asked Sep 30, 2014 at 7:12 sarah.fergusonsarah.ferguson 3,2672 gold badges25 silver badges34 bronze badges 2
  • what do you mean with "this work" $("base").load("ip.php");, how do this set the href of base? or this: $("head").load("ip.php"); ?? – reyaner Commented Sep 30, 2014 at 7:26
  • load doesn't change the href attribute, it loads content from your ip.php and puts it into baseelement, if you want to change href you can do $('base').attr('href', 'ip.php') – paulitto Commented Sep 30, 2014 at 7:33
Add a ment  | 

4 Answers 4

Reset to default 3

Try something like this to set href value

   $('base').attr('href','http://www.google.');

Its depend how your file ip.php is

For example

ip.php

<?php 
   echo 'http://www.google.';

?>

Then

$.get('ip.php',function(response){
    $("base").attr("href", response);
});

I don't know exactly, but in my opinion, u got the meaning of the selector wrong. I think [property='href'] is not a valid selector. href is the property you should search for, and not the value.

You can select DOM-elements with jquery using $("base"). Additionally you can use a key-value bination to search for certain attribute-values of your element.

$("a[href$='']")

With this selector you would select elements with a href attribute which ends with ''.

So the point is: you still select the DOM-element and NOT a part of that element. But your search is better specified.

just need :

window.location = "http://yoursite.php";

dont use load, use $.ajax!

$.ajax({
    url: "ip.php"
}).done(function (data) {
    $("base").attr("href", data);
});

本文标签: javascriptjquery set base hrefStack Overflow