gpt4 book ai didi

javascript - 将 Javascript console.log 输出存储到变量,然后在 html 中访问该变量?

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

下面是我的代码。第一部分是 JS 代码,第二部分是 HTML。

$('#table').on('check.bs.table', function (e, row) {
checkedRows.push({First: row.fname, Second: row.sname});
var test = console.log(checkedRows);
document.getElementById("xyz").innerHTML=test;
});


<p id="xyz"></p>

我基本上想将名字和姓氏从表输出到我的 html 页面,但不知何故它不起作用。从表中选择行时,console.log 打印得很好。我想再次访问 console.log 中选定的数据以在我的 html 页面上查看。

最佳答案

使用JSON Stringify 如果您只想用对象测试您的数组:

// Say, after several .push() our array looks like:

var checkedRows = [
{First:"John", Second: "Doe"},
{First:"Mary", Second: "Jane"},
];

// console.log(checkedRows);

document.getElementById("xyz").innerHTML = JSON.stringify(checkedRows, null, 4);
<pre id="xyz"></pre>

如果您只想打印“John Doe”或“Mary Jane”等字符串,则使用

$('#table').on('check.bs.table', function (e, row) {

var person = {First: row.fname, Second: row.sname}; // Store data into variable

checkedRows.push( person ); // Push person Object to checkedRows Array

console.log( checkedRows );
console.log( person );

document.getElementById("xyz").innerHTML = person.First +" "+ person.Second ;
// or use:
document.getElementById("xyz").innerHTML = row.fname +" "+ row.sname ;
});

关于javascript - 将 Javascript console.log 输出存储到变量,然后在 html 中访问该变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630785/

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