4 回答

TA貢獻1842條經驗 獲得超21個贊
而不是alert("X : ",x);
嘗試使用alert("X : " + x);
. 這應該可以解決它。當您在警報函數中放置逗號時,它可能會將其視為另一個參數,因此您必須使用“+”連接。

TA貢獻1827條經驗 獲得超4個贊
變成。var_ const請參閱有關串聯的其他答案。更改為模板文字。
<script>
// window.location.href = 'SomeSite.php'; // THIS WORKS
alert(window.location.href);
const x = window.location.href;
alert(`x: ${x}`)
</script>

TA貢獻1794條經驗 獲得超8個贊
要將一個字符串追加到 JavaScript 中的另一個字符串中,您應該使用+運算符。不能使用逗號。僅當您使用需要多個參數的函數時才放置它。
因為在這里,alert()我認為您正在輸入第二個參數!
例如:
let string1 = "Hello, "; //Define the first variable.
let string2 = "world!"; //And the second one.
alert(string1 + string2);//Show a message and join the two strings together!
這里你可以使用逗號:
<script>
let string = "We hate the earth!";
string = string.replace("hate", "love"); //FYI: replace() is used to replace a sequence of characters by an another.
</script>
所以你的代碼應該是:
<script>
var x = window.location.href;
alert("X : " + x); //Join "X : " and x together!
</script>

TA貢獻1796條經驗 獲得超7個贊
您需要使用 a 來代替逗號來+
正確地將兩個值連接在一起,這只是將x
變量連接到打印中。
x
如果您要單獨打印出來,您將獲得該值,但在您的上下文中,錯誤的連接是問題所在。
- 4 回答
- 0 關注
- 296 瀏覽
添加回答
舉報