问题描述
我有一个如下的脚本
$('.button1').click(function() {
document.location.href=$(this).attr('id');
});
button1具有可变的唯一ID.单击时,页面必须重定向到URL"www.example.com/index.php?id=buttonid
",但是现在页面仅重定向到"button id
".
the button1 has variable unique ids. on click, the page must redirect to url "www.example.com/index.php?id=buttonid
" but now the page is redirecting only to "button id
".
我想在当前URL之前添加字符串"www.example.com/index.php?id=
".我怎样才能做到这一点?
I want to add the string "www.example.com/index.php?id=
" before the current url. How can I make this possible?
推荐答案
$('.button1').click(function() {
window.location = "www.example.com/index.php?id=" + this.id;
});
首先使用window.location
更好,因为根据规范document.location
的值是只读的,可能会导致您在较旧/不同的浏览器中感到头疼.检查说明@ MDC DOM document.location页面
First of all using window.location
is better as according to specification document.location
value was read-only and might cause you headaches in older/different browsers. Check notes @MDC DOM document.location page
第二点-使用attr
jQuery方法获取ID是一种不好的做法-您应该使用直接的本机DOM访问器this.id
,因为分配给this
的值是普通的DOM元素.
And for the second - using attr
jQuery method to get id is a bad practice - you should use direct native DOM accessor this.id
as the value assigned to this
is normal DOM element.
这篇关于转到链接上单击按钮-jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!