gpt4 book ai didi

javascript - 如何从本地存储追加后显示的html中获取div数据?

转载 作者:行者123 更新时间:2023-12-02 22:34:21 25 4
gpt4 key购买 nike

如何从突出显示的行的 var projectdata = localStorage.getItem('createProject') 获取数据。我有控制台记录 projectdata 如下,其中包含多个记录:

.cleaner{float:left; padding:20px 10px 10px 20px;} 
.rows{width: 1000px; min-height: 80px;}
.rowsAlt{
background-color: #edece8;
}

.cols{height: 100%; float:left;}
.col1{width: 100px; float:left; padding:20px 10px 10px 20px;}
.col2{width: 100px; float:left; padding:20px 10px 10px 20px;}
.col3{width: 200px; float:left; padding:20px 10px 10px 20px;}
.col4{width: 150px; float:left; padding:20px 10px 10px 20px;}
.col5{width: 150px; float:left; padding:20px 10px 10px 20px;}
.col6{width: 100px; float:left; padding:20px 10px 10px 20px;}

#row2{
margin-left:20px;
margin-top: 50px;
position: relative;
width: 90%;
margin: 20px 0px 0px 20px;
}

.highlight { background: #ebdb34 !important; }
.projecteditLink{
height:10px;
border-radius: 5px;
background-color: #42f56f;
color:#fff;
}
<div class="row"><div id="row2" class="rows rowsAlt"><div id="projectName_1" class="col1">Sun</div><div id="clientName_1" class="col2">sun</div><div class="col3">lion,wolves</div><div id="projectStartDate_1" class="col4">Nov-2019</div><div class="col5">Mr Rambo</div><div class="cleaner">Added Sun here</div><div class="col6"><a class="projecteditLink" href="">Edit Link</a></div></div></div><div class="row"><div id="row2" class="rows rowsAlt"><div id="projectName_2" class="col1">Moon Project</div><div id="clientName_2" class="col2">earth</div><div class="col3">lion,wolves</div><div id="projectStartDate_2" class="col4">Nov-2019</div><div class="col5">Mr Simmer</div><div class="cleaner">Added Sun here</div><div class="col6"><a class="projecteditLink" href="">Edit Link</a></div></div></div><div class="row"><div id="row2" class="rows rowsAlt"><div id="projectName_3" class="col1">Simple Demo</div><div id="clientName_3" class="col2">earth</div><div class="col3">man,lion</div><div id="projectStartDate_3" class="col4">Nov-2019</div><div class="col5">Mr Test</div><div class="cleaner">Added Simple here</div><div class="col6"><a class="projecteditLink" href="">Edit Link</a></div></div></div>

//希望从变量 projectdata 中获取类似 Project One、SAAS、man 的文本。如何实现这一目标?

   var divCount = 1;
$(document).ready(function(){
document.getElementById('dashboardArea').innerHTML = "";
var data = localStorage.getItem('saveToLocal');
$('#dashboardArea').append(data);
$("body").on('click', '#saveProjecttoDash', function(e) {
var projectdata = localStorage.getItem('createProject');
console.log("Same here:"+projectdata);
// How to get data from `projectdata` html ?
$('#overlay').fadeOut(300);
var someDiv = '<div class="row"><div id="droppable'+divCount+'" class="pull-bottom" style="height:500px;background:#e3e8e7"><h3></h3><span class="clientNameDash"></span><img src="sampleImg.PNG" id="dashboardImage-"><img id="deleteDashboard" src="delete-512.png"></div></div>';
$('#dashboardArea').append(someDiv);
divCount++;
var saveToLocalStorage = document.getElementById('dashboardArea').innerHTML;
localStorage.setItem("saveToLocal",saveToLocalStorage);
});

});

最佳答案

您可以使用此函数将字符串转换为 HTML 对象(包裹在 div 中)

function createElementFromHTML (htmlString) {
var div = document.createElement("div");
div.innerHTML = htmlString.trim();
return div;
}

然后使用getElementsByClassName挑选所需的值

const projectdata = localStorage.getItem('createProject');
const projectDataHTML = createElementFromHTML(projectdata);

const getInnerText= element => element.innerText;
const projectNames = [...projectDataHTML.getElementsByClassName('col1')].map(getInnerText);
const clientNames = [...projectDataHTML.getElementsByClassName('col2')].map(getInnerText);

结果

projectNames ["Sun", "Moon Project", "Simple Demo"]
clientNames ["sun", "earth", "earth"]

类似地,您也可以使用它们的类名(col3、col4...)来挑选其他值

获取突出显示的行记录

const [highlightedRow] = projectDataHTML.getElementsByClassName('highlight');
const highlightedProjectName = highlightedRow.getElementsByClassName("col1")[0].innerText;
const highlightedClientName = highlightedRow.getElementsByClassName("col2")[0].innerText;

关于javascript - 如何从本地存储追加后显示的html中获取div数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58786246/

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