gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:19:08 25 4
gpt4 key购买 nike

我正在尝试创建一个应用程序来跟踪网页,当用户添加新 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();
}

最佳答案

在显示列表时,只需将单选标记与 a 标记括起来

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();
}
<!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 将 URL 转换为 ‘anchor’ 标签并将标签作为字符串存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54954983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com