我正在尝试从网址获取参数值,并在param = 1
时重定向例如:如果我的网页网址是www.example.com& param = 1,则此页面上div 图片标题中的所有链接都应在此末尾附加此参数出口处的网址
<div id="image-header" >
<a href="www.example.com"></a>
<a href="www.example.com"></a>
<a href="www.example.com"></a>
</div>
所以,如果param = 1,如果我点击example.com,那么点击的网址应该转到www.example.com&amp; param = 1。这在JavaScript中是否可行?
先谢谢
答案 0 :(得分:1)
是的,你需要将click事件绑定到所有元素并防止默认,然后将window.location设置为加上你的参数的href属性。
<div id="image-header" >
<a href="http://www.google.com">example 1</a>
<a href="http://www.google.com">example 2</a>
<a href="http://www.google.com">example 3</a>
</div>
<script>
$(function() {
$(document).on('click', 'a', function(e) {
e.preventDefault();
window.location = $(this).attr('href')+location.search;
});
})
</script>