2016-12-11 62 views
-2

我对JavaScript有一个小问题。我想添加字符串(格式:一二三)的列表,但这削减了符号(“ - ”)和每个元素标签添加。我试着解决这个问题,不幸的是没有成功。在这里我的功能:如何使用javascript在列表中添加剪切字符串?

功能listAdd(){

var string = document.forms["form1"].elements["stringAdd"].value; 
if(string != ""){ 
    var array = string.split(""); 
    var eredmeny = array.some(function(v) { return v == "-";}); 
    if(eredmeny){ 
     for(var i = 0; i <array.length; i++){ 
      if(array[i] != "-"){ 
       var newElement = document.createElement('li'); 
      array[i] = array.toString(); 
      newElement.textContent = array[i]; 

      var list = document.getElementById("gyumi"); 
      list.appendChild(list.createTextNode(array[i])); 
      } 
     } 
    } 
    else{ 
     var newElement = document.createElement('li'); 
     newElement.textContent = szoveg; 

     var list = document.getElementById("gyumi"); 
     list.appendChild(newElement); 
     } 



    } 

}

对不起,我可怜的英语,这是我的第一个问题。

+1

我不明白你到底做 – mattias

回答

0

我想你是说你有一个像"one-two-three"一个字符串,并要分割,截至上-并添加导致"one""two""three"到列表中。

如果是这样,您想要使用split("-")而不是split(""),并且您不需要some来检查-。此外,您还可以使用createTextNode把列表中的项目内容(textContent是不是跨浏览器兼容),你只需要得到列表一次:

function listAdd() { 
 
    var string = document.forms["form1"].elements["stringAdd"].value; 
 
    if (string != "") { 
 
    var list = document.getElementById("gyumi"); 
 
    string.split("-").forEach(function(entry) { 
 
     var newElement = document.createElement('li'); 
 
     newElement.appendChild(document.createTextNode(entry)); 
 
     list.appendChild(newElement); 
 
    }); 
 
    } 
 
} 
 
listAdd();
<form name="form1"> 
 
    <input type="text" name="stringAdd" value="one-two-three"> 
 
</form> 
 
<ul id="gyumi"></ul>

即使工作有字符串中没有-,因为split("-")将数组中是否有字符串中没有-只返回一个元素。

+1

谢谢你的回答! :)它解决了我的问题。 –

1

我在这里的假设是,你想从一个由分隔字符串创建li项目“ - ”字符。如果这是真的,请参阅以下内容:

您可以按照您使用的.split()函数将字符串拆分为它的子组件,然后遍历这些元素并创建列表项并将它们附加到文档主体。

var mystr = 'one-two-three'; 
 

 
var components = mystr.split('-'); 
 

 
components.forEach(function(comp){ 
 
    var item = document.createElement('li'); 
 
    item.textContent = comp; 
 
    // set your attrs on the item here 
 
    document.body.appendChild(item); 
 
});

+0

的OP的使用'textContent'良好。不要为此使用'innerHTML'。 – trincot

+0

谢谢你的回答。这解决了我的问题。 –

0

你可以用split做到这一点,但如果你通过-拆分变得轻松了许多。然后你就可以得到你想要添加到列表中的值。

其次,如果你在运行时表单提交,或在表单按钮的点击此功能,请确保取消表单提交。

最后,你会想在输入清除时,该项目已被添加到列表中。

function listAdd() { 
 
    var inp = document.forms["form1"].elements["stringAdd"]; 
 
    var list = document.getElementById("gyumi"); 
 
    var string = inp.value; 
 
    if (string != "") { 
 
     string.split('-').forEach(function (item) { // split by the hyphen 
 
      var newElement = document.createElement('li'); 
 
      newElement.textContent = item; 
 
      list.appendChild(newElement); // not a text node 
 
     }); 
 
    } 
 
    // clear input once you have added the items: 
 
    inp.value = ''; 
 
    return false; // cancel form submission 
 
}
<form id="form1" onsubmit="return listAdd()"> 
 
    Items: <input name="stringAdd"> [Press enter to add] 
 
</form> 
 
<ul id="gyumi"></ul>

相关问题