简体   繁体   English

HTML5 sessionStorage:变量未存储在 <li> onclick属性

[英]HTML5 sessionStorage : variable not stored on <li> onclick attribute

i wonder why this onclick work on this div: 我想知道为什么这个onclick可以在这个div上工作:

<div id="block-block-contact" class="block-factice expanded" onclick="sessionStorage.contactclass=this.className ">

but not on this li 但不是这个李

 <li id="dhtml_menu-1895" class="first collapsed dhtml-menu collapsed start-collapsed menu-1895" onclick="sessionStorage.menu1895=this.className">

meaning when i click on the div, it does store the contactclass variable whereas when i click on the li, it does not store the menu1895 variable 意思是当我单击div时,它确实存储了contactclass变量,而当我单击li时,它不存储menu1895变量。

Matoeil, 马托埃

Can you please check this https://jsfiddle.net/612ee2df/ 能否请您检查一下https://jsfiddle.net/612ee2df/

 <ul>
  <li id="dhtml_menu-1895" class="first collapsed dhtml-menu collapsed    start-collapsed menu-1895" onclick="sessionStorage.menu1895=this.className">
   Test
  </li>
</ul>

jQuery jQuery的

$(document).ready(function(e)
{
  $('ul li').on('click',function(e)
  {
      var cls = $(this).attr('class');
      alert(cls);
      sessionStorage.menu1895 = cls;
  })
 })

Because I tried your code-snippet but instead of JavaScript I used jQuery & it saved the value in session storage. 因为我尝试了您的代码段,但没有使用JavaScript,所以使用了jQuery,并将其值保存在会话存储中。

another event was linked to this element. 另一个事件与此元素相关联。 To strip all event listeners i did 剥离我所做的所有事件监听器

var old_element=document.getElementById("dhtml_menu-1895");     
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);  

and now it works 现在可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM