Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建';n';用户通过“输入”输入的列表项的数量;提示();_Javascript_Jquery_Html - Fatal编程技术网

Javascript 创建';n';用户通过“输入”输入的列表项的数量;提示();

Javascript 创建';n';用户通过“输入”输入的列表项的数量;提示();,javascript,jquery,html,Javascript,Jquery,Html,我不希望替换无序列表的列表项,并在单击后在其位置生成“n”个列表项(n是用户在单击列表项时在提示下输入的) 我参考了以下链接,但仍然无法为我的案例找到解决方案: 这是我尝试过的,但不起作用: HTML: JavaScript: function inputNumber() { var index = $(this).index(); // to record the index of the clicked item var

我不希望替换无序列表的列表项,并在单击后在其位置生成“n”个列表项(n是用户在单击列表项时在提示下输入的)

我参考了以下链接,但仍然无法为我的案例找到解决方案:

这是我尝试过的,但不起作用:

HTML:

JavaScript:

function inputNumber() {

    var index = $(this).index();   
                     // to record the index of the clicked item 

    var inputNum = prompt("Divide By:");    
                     // input and store 'n' by the user

    var oldList = document.getElementById("list");  
                    // the main old list

    var garbage= oldList.removeChild(oldList.childNodes[index+1]); 
                   // remove the clicked item from the main list 

    var newList=document.createElement("li");  
                  // create new list item

    var i;

    for (i=1;i<=inputNum;i++)
    {
    list.append(newList.clone().text('list-'+i));
     // loop to clone and append 'n' list items in place of the previous one
    }

  }
函数inputNumber(){
var index=$(this.index();
//记录已单击项的索引的步骤
var inputNum=prompt(“除以”);
//由用户输入并存储“n”
var oldList=document.getElementById(“列表”);
//主要旧名单
var garbage=oldList.removeChild(oldList.childNodes[index+1]);
//从主列表中删除单击的项目
var newList=document.createElement(“li”);
//创建新列表项
var i;
对于(i=1;i

代码中存在一些问题。其中一个问题是您忘记了仅用于jQuery的函数的
$()
。您也没有正确删除子元素,jQuery函数
$(this)
.remove()
和JavaScript
。子元素一起工作,只删除单击的元素。您也只创建了一个要添加到列表中的新元素,但需要创建多个,所以我在for循环中添加了这个元素

$("li").click(function() {
  var inputNum = prompt("Divide By:");
  var oldList = document.getElementById("list");
  for (var i=1;i<=inputNum; i++)
  {
  var newList=document.createElement("li");
  $(this).parent().append($(newList).text('list-'+i));
  }
  $(this).remove();
});
$(“li”)。单击(函数(){
var inputNum=prompt(“除以”);
var oldList=document.getElementById(“列表”);

对于(var i=1;我非常感谢您的帮助,但这并不是删除“已单击”的列表项。并且该列表项不会被用户输入的“n”项“替换”。它们将与之前的所有项一起“添加”。不过,非常感谢您的帮助:)你应该在你的问题中提到你的要求,对此我深表歉意。我已经做了编辑。谢谢你的更正。:)我已经对这个问题进行了编辑。我真的很抱歉没有明确提到要求。如果你不介意,请参考编辑。非常感谢你的帮助。我只想删除“单击”的子项,而不是全部。对不起,我忘了更新我的小提琴,请再次检查。在你的代码中(不是JSIDdle,而是这里包含的。您的JSIDdle不包含相同的代码),“单击”列表项正被“n”项正确替换,但其位置也在更改。“单击”列表项应该与“单击”列表项位于同一位置。但它几乎完成了。非常感谢您的帮助。我从几天起就一直坚持这一点!谢谢!:)
function inputNumber() {

    var index = $(this).index();   
                     // to record the index of the clicked item 

    var inputNum = prompt("Divide By:");    
                     // input and store 'n' by the user

    var oldList = document.getElementById("list");  
                    // the main old list

    var garbage= oldList.removeChild(oldList.childNodes[index+1]); 
                   // remove the clicked item from the main list 

    var newList=document.createElement("li");  
                  // create new list item

    var i;

    for (i=1;i<=inputNum;i++)
    {
    list.append(newList.clone().text('list-'+i));
     // loop to clone and append 'n' list items in place of the previous one
    }

  }
$("li").click(function() {
  var inputNum = prompt("Divide By:");
  var oldList = document.getElementById("list");
  for (var i=1;i<=inputNum; i++)
  {
  var newList=document.createElement("li");
  $(this).parent().append($(newList).text('list-'+i));
  }
  $(this).remove();
});