gpt4 book ai didi

javascript - 动态创建 DIV 后超链接不起作用

转载 作者:行者123 更新时间:2023-11-28 02:28:11 24 4
gpt4 key购买 nike

我有一个应用程序可以在 localStorage 中保存用户输入的位置。当用户转到应用程序中保存的位置页面时,JavaScript 会为每个保存的位置创建 div 并显示关键文本。我希望能够单击每个位置并运行一个函数。我坚持让 div 具有超链接。我认为问题出在我的循环中。 “JavaScript:Void(0)”目前只是一个占位符。这是我目前所拥有的:

myApp.onPageInit('saved_locations', function (page) {

var fragment = document.createDocumentFragment();
var parent = document.getElementById("saved");


// iterate localStorage
for (var i = 0; i < localStorage.length; i++) {

// set iteration key name
var key = localStorage.key(i);

// use key name to retrieve the corresponding value
var value = localStorage.getItem(key);

// console.log the iteration key and value
console.log('Key: ' + key + ', Value: ' + value);

//var idNum = i.toString();

let node = document.createElement("div");
let a = document.createElement("a");
a.textContent = key;
a.href = "JavaScript:Void(0)";

node.appendChild(a);


fragment.appendChild(node);

};

parent.appendChild(fragment);

var myForm = document.getElementById("enter_location");

myForm.addEventListener('submit', function saveSearchLocation() {

var lat = document.getElementById('Latitude').value;
var lon = document.getElementById('Longitude').value;
var locationStr = document.getElementById('Location').value;

//Save location parameters to local storage
savedLocationParams = [lat, lon, locationStr];
window.localStorage.setItem(locationStr, JSON.stringify(savedLocationParams));

document.getElementById("saved").onsubmit = function(){
window.location.reload(true);
}

});





});

这是 HTML 页面:

<body>
<div class="pages">
<div data-page="saved_locations" id="saved" class="page navbar-through no-
toolbar" align="center">
<h2><br><u>Enter A Location<br><br></u>
<form id="enter_location">
Latitude: <input type="text" id="Latitude" value=""><br>
Longitude: <input type="text" id="Longitude" value=""><br>
Location: <input type="text" id="Location" value=""><br>
<input type="submit" value="Submit">
</form>
<h2><u>Saved Locations</u></h2>
</div>
</div>

最佳答案

您必须创建a 元素,然后将您想要的文本分配给它们,然后您必须将a 元素放入div 中。

let a = document.createElement("a");
a.textContent = "this is the link text";
a.href = "JavaScript:Void(0)";
node.appendChild(a);

如果你想链接值文本,你可以这样做而不是创建文本节点:

a.textContent = key;

我稍微修改了您的代码以显示示例。只需单击“单击此处”即可查看该功能的效果:

//myApp.onPageInit('saved_locations', function (page) {
var clickMeToDoLoad = function() {
var fragment = document.createDocumentFragment();
var parent = document.getElementById("saved");


var fakeLocalStorage = {
"key0": "value0",
"key1": "value1",
"key2": "value2"
};

// iterate localStorage
//for (var i = 0; i < localStorage.length; i++) {
for (var i = 0; i < 3; i++) {
// set iteration key name
//var key = localStorage.key(i);

// use key name to retrieve the corresponding value
//var value = localStorage[key);

// set iteration key name
let key = Object.keys(fakeLocalStorage)[i];

// use key name to retrieve the corresponding value
let value = fakeLocalStorage[key];

// console.log the iteration key and value
console.log('Key: ' + key + ', Value: ' + value);

let idNum = i.toString();

let node = document.createElement("div");
let a = document.createElement("a");
a.textContent = key;
a.href = "JavaScript:Void(0)";
node.appendChild(a);
fragment.appendChild(node);

}

parent.appendChild(fragment);
}
<div class="pages">
<div data-page="saved_locations" id="saved" class="page navbar-through no-
toolbar" align="center">
<h2><br/><u>Enter A Location<br /><br /></u></h2>
<form>
Latitude: <input type="text" name="Latitude" value=""><br> Longitude: <input type="text" name="Longitude" value=""><br> Location: <input type="text" name="Location" value=""><br>
<input type="submit" value="Submit" onclick="return false;">
</form>
<h2><br /><u>Saved Locations</u></h2>
<button onclick="clickMeToDoLoad();">Click to demo function</button>
</div>
</div>

关于javascript - 动态创建 DIV 后超链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52631110/

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