Javascript jQuery数组的concat数据
我有一个数组包含许多像这样的Javascript jQuery数组的concat数据,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个数组包含许多像这样的选项标记 如何concat将它们转换为字符串,然后将其转换为select块 像这样 var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">'; // Maybe write something to concat array into string ? select_block = select_block + option
选项
标记
如何concat
将它们转换为字符串,然后将其转换为select
块
像这样
var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">';
// Maybe write something to concat array into string ?
select_block = select_block + options; // options is an array contains many option tags
select_block = select_block + '</select>';
var select_block='';
//也许可以将concat数组中的内容写入字符串?
选择块=选择块+选项;//选项是包含许多选项标记的数组
选择_块=选择_块+“”;
如果使用jQuery,根本不需要将它们连接到字符串中。您只需附加DomeElement数组即可,它们是:
var options = [ /* your option elements */ ];
var $select_block = $('<select id="mission_reward" name="mission_reward" class="select_reward" />');
$select_block.append(options);
var options=[/*您的选项元素*/];
变量$select_block=$('');
$select_block.append(选项);
如果使用jQuery,根本不需要将它们连接到字符串中。您只需附加DomeElement数组即可,它们是:
var options = [ /* your option elements */ ];
var $select_block = $('<select id="mission_reward" name="mission_reward" class="select_reward" />');
$select_block.append(options);
var options=[/*您的选项元素*/];
变量$select_block=$('');
$select_block.append(选项);
要使用非jQuery JavaScript创建HTML字符串,可以使用以下方法:
// here we create a string of HTML, using a String literal,
// and concatenate that String with another:
var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' +
// here we use Array.prototype.join() to combine the
// Array elements of the options Array (an Array of
// Strings) together using a zero-length string:
options.join('') +
// and then concatenate the String-so-far with the final
// String to close the resulting <select> element:
'</select>';
//这里我们使用字符串文字创建一个HTML字符串,
//并将该字符串与另一个字符串连接:
var html=''+
//这里我们使用Array.prototype.join()来组合
//选项数组的数组元素(一个
//字符串)一起使用零长度字符串:
选项。加入(“”)+
//然后将字符串连接到最后一个
//用于关闭结果元素的字符串:
'';
//创建选项数组:
变量选项=['opt1'、'opt2'、'opt3']
var html=''+options.join('')+'';
document.body.insertAdjacentHTML('beforeend',html)代码>要使用非jQuery JavaScript创建HTML字符串,可以使用以下方法:
// here we create a string of HTML, using a String literal,
// and concatenate that String with another:
var html = '<select id="mission_reward" name="mission_reward" class="select_reward">' +
// here we use Array.prototype.join() to combine the
// Array elements of the options Array (an Array of
// Strings) together using a zero-length string:
options.join('') +
// and then concatenate the String-so-far with the final
// String to close the resulting <select> element:
'</select>';
//这里我们使用字符串文字创建一个HTML字符串,
//并将该字符串与另一个字符串连接:
var html=''+
//这里我们使用Array.prototype.join()来组合
//选项数组的数组元素(一个
//字符串)一起使用零长度字符串:
选项。加入(“”)+
//然后将字符串连接到最后一个
//用于关闭结果元素的字符串:
'';
//创建选项数组:
变量选项=['opt1'、'opt2'、'opt3']
var html=''+options.join('')+'';
document.body.insertAdjacentHTML('beforeend',html)代码>您可以使用jQuery()
var select=$(“”{
id:“任务奖励”,
名称:“任务奖励”,
“类”:“选择奖励”,
html:选项
});
您可以使用jQuery()
var select=$(“”{
id:“任务奖励”,
名称:“任务奖励”,
“类”:“选择奖励”,
html:选项
});
您想为阵列的每个选项选择一个选项吗?那么您肯定要应用forEach()
方法:
var opt = ['option A', 'Option B', 'Option C'];
var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">';
opt.forEach(function(element) {
// crude code follows ;)
select_block +="<option value='"+element+"'>"+element+"</option>";
});
select_block +="</select>";
var opt=['option A'、'option B'、'option C'];
var select_块=“”;
opt.forEach(函数(元素){
//粗代码如下;)
选择_block+=“”+element+“”;
});
选择_block+=“”;
您想为阵列的每个选项选择一个选项吗?那么您肯定要应用forEach()
方法:
var opt = ['option A', 'Option B', 'Option C'];
var select_block = '<select id="mission_reward" name="mission_reward" class="select_reward">';
opt.forEach(function(element) {
// crude code follows ;)
select_block +="<option value='"+element+"'>"+element+"</option>";
});
select_block +="</select>";
var opt=['option A'、'option B'、'option C'];
var select_块=“”;
opt.forEach(函数(元素){
//粗代码如下;)
选择_block+=“”+element+“”;
});
选择_block+=“”;
您希望在末尾有一个字符串,还是实际的选择带有这些选项的对象?您的数组是否包含描述选项的字符串,或者文档中的实际选项
元素?是否希望在末尾包含字符串,或者包含这些选项的实际选择
对象?数组中是否包含描述选项的字符串,或者文档中的实际选项元素?