在我的代码中,我有这样的语句:
document.location.search="?a="+n;
我正在尝试缩短(代码高尔夫)它,这样我就不必每次都输入 document.location.search
。
我首先想到的是将 document.location.search
设置为一个变量,然后设置该变量,如下所示:
s=document.location.search;
s="?a="+n;
这当然行不通,因为 s 是由 document.location.search
的值设置的,而不是它的引用。有没有办法通过引用设置 s ?有没有其他方法可以消除 document.location.search
的重复实例?
最佳答案
写一个函数
function s(val) {
document.location.search = "?a=" + val;
}
并调用
s('test');
关于javascript - 设置对 document.location.search 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979303/