Javascript将URL转换为“ anchor”标签,并将标签作为字符串存储在数组中

时间:2019-03-02 03:36:30

标签: javascript html css

我正在尝试创建一个可跟踪网页的应用程序,并随着用户添加新URL,链接将出现在输入框下方的网页上。 我想将URL转换为“ anchor”标签,然后将标签作为字符串存储在数组中。 我可以获得输出,但是我不知道如何将url转换为achor标签。 这就是我的html和javascript。 总体而言,我正在尝试修改超级链接,以便当用户选择链接时,页面将显示在新标签中。要启用此链接,例如‘’标签中的‘target =“ _ blank”。

<!DOCTYPE html>
<html>
<head>
<title>Track your favorite websites</title>
<meta name="author" content="791894" >
<meta name="date" content="2019-03-01T16:33:43-0700" >
<script type="text/javascript" src="partA.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Please Complete the Following Registration</h2>

<form id="register" onsubmit="addToArray();return false">
<table>

    <tr><td>Enter Your URL Here</td><td><input type="text" id="url" size="25" required="true"></td></tr>

</table>
<input type="submit" value="Submit">
</form>
<h3><span id="showlist"></span></h3>
</body>
</html>

--------- javascript ------------

var objectarray=[];//array


function addToArray() {
    //read items from form and create client object
    var clientobject={url};
    //input variables into clientobject

    clientobject.url=document.getElementById("url").value;
    //alert("Client Name: "+clientobject.firstname+" "+clientobject.lastname);
    //load into objectarray
    objectarray.push(clientobject);
    displayList();//display object array

}

function displayList() {
    //variables
    var clientlist="";//this will be the list of elements in the array list
    var displayRadiobuttons="";//display elements as a list of radio buttons

        for (var i=0;i<objectarray.length;i++) 
        {
            //local instance of clientobject
            var clientobject={url};
            clientobject=objectarray[i];
            clientlist= clientobject.url;
            //create radio button tags and elements
            displayRadiobuttons+="<input type=radio name=listitem ";
            displayRadiobuttons+=" value="+i+" ";
            displayRadiobuttons+=" onchange=deleteItem(this.value)>";
            displayRadiobuttons+=clientlist+"<br>";


        }
        //display list
        document.getElementById("showlist").innerHTML=displayRadiobuttons;

}

//delete item from objectarry at index i using splice
function deleteItem(i) {
    //delete ONE  item at index i   
    objectarray.splice(i,1);
    //display modified list
    displayList();
}

1 个答案:

答案 0 :(得分:0)

在显示列表时,只需将单选标签包含在标签中即可

{% if book.average_score %}
  {% for s in range(book.average_score|round(mode='ceil')) %}
     <i class="fas fa-star"></i>
  {% endfor %}
{% endif %}
var objectarray=[];//array


function addToArray() {
    //read items from form and create client object
    var clientobject={url};
    //input variables into clientobject

    clientobject.url=document.getElementById("url").value;
    //alert("Client Name: "+clientobject.firstname+" "+clientobject.lastname);
    //load into objectarray
    objectarray.push(clientobject);
    displayList();//display object array

}

function displayList() {
    //variables
    var clientlist="";//this will be the list of elements in the array list
    var displayRadiobuttons="";//display elements as a list of radio buttons

        for (var i=0;i<objectarray.length;i++) 
        {
            //local instance of clientobject
            var clientobject={url};
            clientobject=objectarray[i];
            clientlist= clientobject.url;
            //create radio button tags and elements
            displayRadiobuttons+="<a href=https://"+clientobject.url+" target='_blank'><input type=radio name=listitem ";
            displayRadiobuttons+=" value="+i+" ";
            displayRadiobuttons+=" onchange=deleteItem(this.value)>";
            displayRadiobuttons+=clientlist+"</a><br>";


        }
        //display list
        document.getElementById("showlist").innerHTML=displayRadiobuttons;

}

//delete item from objectarry at index i using splice
function deleteItem(i) {
    //delete ONE  item at index i   
    objectarray.splice(i,1);
    //display modified list
    displayList();
}