通过localStorage在两个页面之间传递click事件

Hob*_*obo 10 javascript html5 local-storage

我想从第2页开始点击事件.第1页第2页链接,点击按钮时有一个按钮,它在第1页上添加了一行HTML.我正在尝试用于传递数据.我的代码不起作用,请看下面:localStorage

1st Page.html


HTML

<div id="content">
</div>
Run Code Online (Sandbox Code Playgroud)

JS

var     output = document.getElementById('content');

addEvent(window, 'storage', function (event) {
  if (event.key == 'StorageName') {
    output.innerHTML = event.newValue;
  }
});
Run Code Online (Sandbox Code Playgroud)

第2页

HTML

<input id="data" type="button" value="+" onclick="addRow()">
Run Code Online (Sandbox Code Playgroud)

JS

addEvent(dataInput, 'keyup', function () {
localStorage.setItem('StorageName', this.value);
});

var dataInput = dataInput = document.getElementById('data');
object.onclick = function(){
addRow() {
var div = document.createElement('div');

div.className = 'row';

div.innerHTML = '<button>GO</button>';

document.getElementById('content').appendChild(div);
}
};
Run Code Online (Sandbox Code Playgroud)

sky*_*000 3

您还没有addRow()正确定义您的功能。函数的名称是用关键字 定义的function,而不是在函数体内。你的代码:

object.onclick = function(){
addRow() {
var div = document.createElement('div');

div.className = 'row';

div.innerHTML = '<button>GO</button>';

document.getElementById('content').appendChild(div);
}
};
Run Code Online (Sandbox Code Playgroud)

应改为:

function addRow() {
    var div = document.createElement('div');

    div.className = 'row';

    div.innerHTML = '<button>GO</button>';

    document.getElementById('content').appendChild(div);
}
object.onclick = addRow;
Run Code Online (Sandbox Code Playgroud)