Jquery获取url值不起作用

时间:2014-12-14 15:21:00

标签: javascript jquery url path

今天,我来问一个关于小javascript问题的帮助。 url如何使用这个jquery

示例网址

  

http://mywebsite.com/?link=f9891188&title=My_Post_Title&dLink1=http://url.com/one&dLink2=http://url.com/two&dLink3=http://url.com/three&dLink4=http://url.com/four&gplay=1234

这个html和javascript

function GetUrlValue(a) {
	var b = window.location.search.substring(1);
	var c = b.split('&');
	for (var i = 0; i < c.length; i++) {
		var d = c[i].split('=');
		if (d[0] == a) {
			return d[1]
		}
	}
}
$('#title').html('<h2>' + GetUrlValue('title') + '</h2>');
if (geoip_country_code() == 'TK' || geoip_country_name() == 'Tokelau') {}
else {
	if (GetUrlValue('dLink1') == '') {
		$('#url1').html('No link')
	} else {
		$('#url1').attr('onclick', "this.href='" + GetUrlValue('dLink1') + "'")
	}
	if (GetUrlValue('dLink2') == '') {
		$('#url2').html('No link')
	} else {
		$('#url2').attr('onclick', "this.href='" + GetUrlValue('dLink2') + "'")
	}
	if (GetUrlValue('dLink3') == '') {
		$('#url3').html('No link')
	} else {
		$('#url3').attr('onclick', "this.href='" + GetUrlValue('dLink3') + "'")
	}
	if (GetUrlValue('dLink4') == '') {
		$('#url4').html('No link')
	} else {
		$('#url4').attr('onclick', "this.href='" + GetUrlValue('dLink4') + "'")
	}
	$('#gp').attr('onclick', "this.href='https://play.google.com/store/apps/details?id=" + GetUrlValue('gplay') + "'");
}
$(document).ready(function () {
	document.title = ('' + GetUrlValue('title') + '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="http://j.maxmind.com/app/country.js" charset="ISO-8859-1"></script>

<section>
<div id='container_buttons'>
<p>
<a class='a_btn 1' href='#' id='url1' onclick='' rel='nofollow' target='_blank' title='Download'>Download 1</a>
</p>
<p>
<a class='a_btn 2' href='#' id='url2' onclick='' rel='nofollow' target='_blank' title='Download'>Download 2</a>
</p>
<p>
<a class='a_btn 3' href='#' id='url3' onclick='' rel='nofollow' target='_blank' title='Download'>Download 3</a>
</p>
<p>
<a class='a_btn 4' href='#' id='url4' onclick='' rel='nofollow' target='_blank' title='Download'>Download 4</a>
</p>
</div>
</section>

1 个答案:

答案 0 :(得分:0)

我猜测你想要什么,但如果你试图从查询字符串中读取值并相应地设置锚标签href属性,你可以按照以下步骤进行。

这是从查询字符串中提取值的一个很好的函数:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

参考:How can I get query string values in JavaScript?

完成剩下的工作后,您可以通过选择if/else以url开头的所有a元素,然后相应地设置id来稍微干掉href语句。 / p>

$("a[id^='url']").each(function(){
  var linkParam = "dLink" + this.id.replace("url", "");

  if (getParameterByName(linkParam)) {
    this.href= getParameterByName(linkParam);
  } else {
    $(this).text('No link')
  }
});

对于以下查询字符串(请注意缺少dLink4):

http://example.com/?link=f9891188&title=My_Post_Title&dLink1=http://url.com/one&dLink2=http://url.com/two&dLink3=http://url.com/three

您获得以下标记:

<section>
  <div id="container_buttons">
    <p>
      <a class="a_btn 1" href="http://url.com/one" id="url1">Download 1</a>
    </p>
    <p>
      <a class="a_btn 2" href="http://url.com/two" id="url2">Download 2</a>
    </p>
    <p>
      <a class="a_btn 3" href="http://url.com/three" id="url3">Download 3</a>
    </p>
    <p>
      <a class="a_btn 4" href="#" id="url4">No link</a>
    </p>
  </div>
</section>