名称href标签根据用户文本输入JavaScript

时间:2016-04-30 02:11:18

标签: javascript jquery html5

在我的应用中,我让用户添加类别。每个类别都是带有标签的简单href,用户输入该标签作为类别名称。我遇到的问题是显示类别名称(未定义)。所以我不确定问题出在哪里。此外,当我再次点击添加类别时,之前创建的一个消失了!

var boxName="type here"; 
var inputt = document.getElementById("boxName").value;
function addInput()
{
    // var boxName="type here"; 

document.getElementById('responce').innerHTML='<br/><input  type="text" id="'+boxName+'" value="'+boxName+'" /><input type="button" onclick="addlinking()" value="Add"/><span id="Adding"></span>';
var inputt = document.getElementById("boxName").value;
addlinking(inputt);

}
function addlinking(tt){
    document.getElementById('Adding').innerHTML = '<br/><input type="submit" onclick="addinghref()" value="'+tt+'"><i class="fa fa-angle-right"></i></a><span id="Linking"></span>';
 
}
function addinghref()
{

    document.getElementById('Linking').innerHTML='<a href="http://google.com"></a>';
}
<input type="button" onclick="addInput()" value="Add Category"> 
                       <span id="responce"></span>

2 个答案:

答案 0 :(得分:0)

var boxName = "type here";

var id = boxName.replace(/\s/g, '_');

function addInput() {
  document.getElementById('responce').innerHTML = '<br/><input  type="text" id="' + id + '" value="' + boxName + '" /><input type="button" onclick="addlinking()" value="Add"/><span id="Adding"></span>';
}

function addlinking(tt) {
  var inputt = document.getElementById(id).value;
  document.getElementById('Adding').innerHTML = '<br/><input type="submit" onclick="addinghref()" value="' + inputt + '"><i class="fa fa-angle-right"></i></a><span id="Linking"></span>';
}

function addinghref() {
  document.getElementById('Linking').innerHTML = '<a href="http://google.com">Link</a>';
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">


<input type="button" onclick="addInput()" value="Add Category">
<span id="responce"></span>

答案 1 :(得分:0)

&#13;
&#13;
.insert
&#13;
int vertices = 100;
LinkedList<Integer>[] AdjList = new LinkedList[vertices+1];


for (i = 1; i <= vertices; ++i) {         
    for (j = 1; j <= 6 && j + i <= vertices; ++j) {

        AdjList[i].add(j);    //Error occurs here.
    }
   }
&#13;
var catTemplate = document.getElementById("adding-template")
                          .content
                          .querySelector(".category");
var createCatDiv = document.getElementById('create-cat');
var createCatInput = createCatDiv.querySelector("input[type=text]");

function addInput()
{
  // Clear previous entry.
  createCatInput.value = "";
  // Show the div.
  createCatDiv.classList.remove("hidden");
}

function addlinking()
{
  // Hide the create cat div.
  createCatDiv.classList.add("hidden");
  // Import the category div from the template.
  var catDiv = document.importNode(catTemplate, true);
  document.getElementById("response").appendChild(catDiv);
  // Set the input.
  var input = catDiv.querySelector("input");
  input.value = createCatInput.value;
  // Replace duckduckgo by the address of your link.
  input.onclick = location.assign.bind(location, "https://duckduckgo.com");
}
&#13;
&#13;
&#13;