考虑到这是HTML代码,
function changeColor(){
var selectedColor = document.getElementById("selectColor").value;
if(selectedColor != "selectTheColor"){
document.bgColor = selectedColor;
document.cookie = "color="+selectedColor+";expires=Fri, 2 Mar 2018 02:00:00 UTC";
var splited = document.cookie.split("="); // codes here not working
alert(splited[1]);
}
}
<select value="bgColor" id="selectColor" onchange="changeColor();">
<option value="selectTheColor">Select color</option>
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
</select>
为什么代码无法拆分?
最佳答案
这种方式不支持在多于一行的字符串上分配字符串(使用”,则必须使用``)。而是执行以下操作:
function changeColor(){
var selectedColor = document.getElementById("selectColor").value;
if(selectedColor != "selectTheColor"){
document.bgColor = selectedColor;
var test = "color="+selectedColor+`;expires=Fri, 2 Mar 2018
02:00:00 UTC`;
var splited = test.split("="); // codes here not working
alert(splited[1]);
}
}
<select value="bgColor" id="selectColor" onchange="changeColor();">
<option value="selectTheColor">Select color</option>
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
</select>
关于javascript - Cookie的分割字符串不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49056818/