所以我的HTML中有一个具有以下属性的元素(我使用文档就绪函数创建元素)
a_elem.setAttribute("href", "kitchen.html");
a_elem.setAttribute("onclick", "passId(event, this)");
所以我想做的是将ID传递给kitchen.html。我使用passId函数执行以下操作:
function passId(event, aelem){
console.log("EEMMM ESTOYYY ");
var roomname = aelem.closest("figure").querySelector("figcaption").innerHTML;
console.log("Voy a bbuscar con roomname = " + roomname);
api.rooms.getRooms().done(function(data) {
$.each(data, function(i, item){
if(item.name == roomname){
var room_id = item.id;
console.log("FOUND IT, ID is " + room_id);
window.localStorage.clear();
window.localStorage.setItem("room_id", room_id);
window.location.href = 'kitchen.html';
}
});
});
}
然后在kitchen.html中我需要使用该ID,但事实证明它有时太晚了(或者我认为这样)并且出现异常现象:
$(document).ready(function() {
var roomid = window.localStorage.getItem("room_id");
window.localStorage.clear();
api.rooms.getRoom(roomid).done(function(data){
...
});
});