【问题标题】:Jquery get url value doesn't workJquery获取url值不起作用
【发布时间】:2015-02-12 18:19:16
【问题描述】:

今天,我来请你帮忙解决一个小的 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>

【问题讨论】:

  • 您的问题不清楚。您能否详细说明您正在尝试做什么?

标签: javascript jquery url path


【解决方案1】:

我猜测你想要什么,但如果你试图从查询字符串中读取值并相应地设置锚标签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?

剩下的部分,您可以通过选择任何a 元素(其id 以url 开头),然后相应地设置href 来稍微干燥您的if/else 语句:

$("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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2023-03-08
    • 2014-12-29
    • 2017-02-02
    • 1970-01-01
    • 2012-01-28
    • 2014-07-01
    相关资源
    最近更新 更多