gpt4 book ai didi

javascript - 如何在不删除任何样式/CSS/图形的情况下更改网页内容

转载 作者:行者123 更新时间:2023-12-03 09:33:48 25 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 或 jQuery 通过刷新按钮更改页面中的内容。我想更改表格中的内容,但我使用的任何命令都会删除表格的部分内容并将新内容保留为白色。

到目前为止,我已经尝试过 change()replaceWith()load()text() 但所有这些都会删除表格的部分内容。使用 document.write() 它会在新页面中打印。

我可以使用任何命令来用新内容替换已经写入的表格吗?

更新:

代码

<script>
var result = [];
var k = 0;
var jsonObject = JSON.parse(eventsJson);
$(document).ready(function(){
$("#priceInfoB").on("click", function(){
//alert(jsonObject.events.event[0].name);
for (i=0;i<4;i++){
if (jsonObject.events.event[i].isFree == "true"){
result[k] = jsonObject.events.event[i].name;
k++;
}
}
$("#events").text(result[0] + result[1]);
$('#myTable').remove();
});
});

<table id="myTable">
<tr class="head">
<th></th>
<th>New York</th>
<th>Chicago</th>
<th>San Francisco</th>
</tr>
<tr>
<th>BLABLA</th>
<td>Sat, 4 Feb 2012<br />11am - 2pm</td>
<td>Sat, 3 Mar 2012<br />11am - 2pm</td>
<td>Sat, 17 Mar 2012<br />11am - 2pm</td>
</tr>

var eventsJson='{"events":{"event":[{"id":"1","name":"A Poetic Perspective","isFree":"true","locations":[{"location":"New York","eventDate":"2015-05-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-05-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Vivamus elementum, diam eget ullamcorper fermentum, ligula libero euismod massa, quis condimentum tellus lacus sit."},{"id":"2","name":"Walt Whitman at War","isFree":"false","locations":[{"location":"New York","eventDate":"2015-07-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-07-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-08-01","eventTime":"15:00"}],"descr":"Donec convallis eu metus eget dictum. Etiam non lobortis dui."},{"id":"3","name":"Found Poems & Outsider Poetry","isFree":"false","locations":[{"location":"New York","eventDate":"2015-06-02","eventTime":"11:00"},{"location":"Chicago","eventDate":"2015-07-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Ut fermentum, elit vel iaculis viverra, dui libero ultrices nibh, ut ornare."},{"id":"4","name":"Natural Death: An Exploration","isFree":"true","locations":[{"location":"New York","eventDate":"2015-05-02","eventTime":"14:00"},{"location":"Chicago","eventDate":"2015-05-01","eventTime":"14:00"},{"location":"San Francisco","eventDate":"2015-06-01","eventTime":"15:00"}],"descr":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent aliquet urna ut tortor consequat."}]}}';

最佳答案

这将从提供​​的 JSON 更新表...

http://jsfiddle.net/ArchersFiddle/eemxvu0z/

HTML

<table id="myTable">
<tr class="head">
<th></th>
<th>New York</th>
<th>Chicago</th>
<th>San Francisco</th>
</tr>
<tr>
<th>BLABLA</th>
<td>Sat, 4 Feb 2012<br />11am - 2pm</td>
<td>Sat, 3 Mar 2012<br />11am - 2pm</td>
<td>Sat, 17 Mar 2012<br />11am - 2pm</td>
</tr>
</table>

Javascript

var data = JSON.parse(eventsJson);

function showEvent(id) {
for (var i in data.events.event) {
if (data.events.event[i].id == id) {
var thisEvent = data.events.event[i];
var head = "<tr class=\"head\"><th></ht>";
var row = "<tr><th>" + thisEvent.name + "</th>";
for (var l in thisEvent.locations) {
var thisLocation = thisEvent.locations[l];
head += "<th>" + thisLocation.location + "</th>";
row += "<td>" + thisLocation.eventDate + "<br/>" + thisLocation.eventTime + "</td>";
}
head += "</tr>";
row += "</tr>";
$("#myTable").html(head + row);
}
}
}

showEvent(4); // for example

基本上,只需根据提供的数据重建表格的 html 并使用 $("#myTable").html(html); 来更新它。我预计您将使用事件(例如选择值更改)来触发表更新,因此只需使用 showEvent(id); 函数调用即可更新它。

关于javascript - 如何在不删除任何样式/CSS/图形的情况下更改网页内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404942/

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