Javascript引用字符串里面?

时间:2014-04-19 00:11:44

标签: javascript string

我似乎对我的JS代码提出了问题。我正在尝试使用双引号创建一个字符串,例如'这是一个" test"字符串',似乎没有任何工作。奇怪的是,我可以制作字符串"这是一个'测试'字符串",它似乎通过罚款。我的代码如下,以及两个显示两个字符串的实例。我的最终目标是在按下生成按钮时获取双引号INSIDE字符串以及函数调用和生成(参见实例)。 谢谢!

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

第二个实例(string =" Kindle Fire HDX 7' 16GB") http://hawkgen.com/gen/

以下是包含字符串的代码:

        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>

3 个答案:

答案 0 :(得分:2)

只是逃避字符串:

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

答案 1 :(得分:0)

您需要使用\

转义它

答案 2 :(得分:0)

"表示以"开头的字符串的结尾。您需要使用反斜杠转义,例如"Kindle Fire HDX 7\" 16GB",或者使用'打开和关闭字符串,这样您就可以在字符串中使用普通",例如'Kindle Fire HDX 7" 16GB'

Values, variables, and literals