gpt4 book ai didi

Javascript 在字符串中引用?

转载 作者:行者123 更新时间:2023-12-02 17:29:53 24 4
gpt4 key购买 nike

我的 JS 代码似乎出现了问题。我正在尝试创建一个带有双引号的字符串,例如“这是一个“测试”字符串”,但似乎没有任何效果。奇怪的是,我可以使字符串“这是一个‘测试’字符串”,而且似乎通过得很好。下面是我的代码以及两个实际示例,显示了两个字符串的运行情况。我的最终目标是在字符串内获取双引号,并在按下生成按钮时调用和生成函数(请参阅实时示例)。谢谢!

第一个实例(字符串 = 'Kindle Fire HDX 7"16GB') http://hawkgen.com/gen2/

第二个实例(字符串 =“Kindle Fire HDX 7' 16GB”) 奥 git _a

这是包含字符串的代码:

        SList.slist2 = {
"amazon": ["Kindle Fire HDX 7' 16GB", 'Kindle Charger', 'Kindle Fire HDX'],
"apple": ['MacBook', 'iMac', 'iPhone', 'iPad'],
"keurig": ['Platinum', 'Vue'],
"nike": ['Fuel Band']
};
SList.scontent = {
"Kindle Fire HDX 7' 16GB": 'kindlefire7',
'Kindle Charger': 'kindlecharge',
'Kindle Fire HDX': 'kindlefirehdx',
'MacBook': 'macbook',
'iMac': 'imac',
'iPhone': 'iphone',
'iPad': 'ipad',
'Platinum': 'platinum',
'Vue': 'vue',
'FuelBand': 'fuelband'
};


如果需要的话,这里是完整的代码:

   <!-- The first select list -->
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="buttons.css">
<link rel="stylesheet" href="buttons-core.css">
<link rel="stylesheet" href="forms.css">
<link rel="stylesheet" href="forms-r.css">
<center>
<form class="pure-form">
<select name="slist1" onchange="SList.getSelect('slist2', this.value);">
<option>- - -</option>
<option value="amazon">Amazon</option>
<option value="apple">Apple</option>
<option value="keurig">Keurig</option>
<option value="nike">Nike</option>
</select>
<!-- Tags for the seccond dropdown list, and for text-content -->
<span id="slist2"></span> <div id="scontent"></div>
<div
<form class="pure-form">
<input type="text" value="Code" readonly id="display">
</form>
</div>
<br>
<input type="button" onclick="SList.getSelect('scontent','go');" class="pure-button pure-button-primary" value="Generate">
</center>
</form>

<script>
var choice2;
function setChoice(value) {
choice2 = value;

}
/* Script Double Select Dropdown List, from: coursesweb.net/javascript/ */
var SList = new Object(); // JS object that stores data for options

// HERE replace the value with the text you want to be displayed near Select
var txtsl2 = '';

/*
Property with options for the Seccond select list
The key in this object must be the same with the values of the options added in the first select
The values in the array associated to each key represent options of the seccond select
*/
SList.slist2 = {
"amazon": ["Kindle Fire HDX 7' 16GB", 'Kindle Charger', 'Kindle Fire HDX'],
"apple": ['MacBook', 'iMac', 'iPhone', 'iPad'],
"keurig": ['Platinum', 'Vue'],
"nike": ['Fuel Band']
};


/*
Property with text-content associated with the options of the 2nd select list
The key in this object must be the same with the values (options) added in each Array in "slist2" above
The values of each key represent the content displayed after the user selects an option in 2nd dropdown list
*/

SList.scontent = {
"Kindle Fire HDX 7' 16GB": 'kindlefire7',
'Kindle Charger': 'kindlecharge',
'Kindle Fire HDX': 'kindlefirehdx',
'MacBook': 'macbook',
'iMac': 'imac',
'iPhone': 'iphone',
'iPad': 'ipad',
'Platinum': 'platinum',
'Vue': 'vue',
'FuelBand': 'fuelband'
};





/* From here no need to modify */

// function to get the dropdown list, or content
SList.getSelect = function(slist, option) {
if (option == 'go') {
option = choice2;
}
document.getElementById('scontent').innerHTML = ''; // empty option-content

if(SList[slist][option]) {
// if option from the last Select, add text-content, else, set dropdown list
if(slist == 'scontent'){;
var selected = SList[slist][option];
functions[selected]();
}
else if(slist == 'slist2') {
var addata = '<option>- - -</option>';
for(var i=0; i<SList[slist][option].length; i++) {
addata += '<option value="'+SList[slist][option][i]+'">'+SList[slist][option][i]+'</option>';
}

document.getElementById('slist2').innerHTML = txtsl2+' <select name="slist2" onchange="setChoice(this.value);">'+addata+'</select>';
}
}
else if(slist == 'slist2') {
// empty the tag for 2nd select list
document.getElementById('slist2').innerHTML = '';
}
}

var functions = {

kindlefire7: function(){
var secondPossible = 'ABCDEFGHJKLMNPQRSVWY123456';
var firstPossible = '123456';
var firstLength = 1;
var secondLength = 2;

var firstString = Array.apply(null, new Array(firstLength)).map(function () {
return firstPossible[Math.floor(Math.random() * firstPossible.length)];
}).join('');
var secondString = Array.apply(null, new Array(secondLength)).map(function () {
return secondPossible[Math.floor(Math.random() * secondPossible.length)];
}).join('');

document.getElementById("display").value='D0FB A0A0 343' + firstString + ' 0A' + secondString

},
kindlecharge: function(){window.alert("func1 called")},
kindlefirehdx: function(){window.alert("func1 called")},
macbook: function(){window.alert("func1 called")},
imac: function(){window.alert("func1 called")},
iphone: function(){window.alert("func1 called")},
ipad: function(){window.alert("func1 called")},
platinum: function(){window.alert("func1 called")},
vue: function(){window.alert("func1 called")},
fuelband: function(){window.alert("func1 called")}
}





</script>

最佳答案

只需转义字符串:

var kindle = "Kindle Fire HDX 7\" 16GB"

关于Javascript 在字符串中引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com