Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将删除的属性添加回悬停方法_Javascript_Jquery - Fatal编程技术网

Javascript 将删除的属性添加回悬停方法

Javascript 将删除的属性添加回悬停方法,javascript,jquery,Javascript,Jquery,我已经使用下面的代码从我的html中删除了样式和id属性,但是我希望在访问者移动到另一个元素后,它们会返回。我对jQuery相当陌生,不知道如何实现它。我真的很感激有人的帮助 <div class="base" style="background-image: url(img/3.jpg);"> <div id="overlay"></div> </div> <script> $('.base').hover(function

我已经使用下面的代码从我的html中删除了样式和id属性,但是我希望在访问者移动到另一个元素后,它们会返回。我对jQuery相当陌生,不知道如何实现它。我真的很感激有人的帮助

<div class="base" style="background-image: url(img/3.jpg);">
  <div id="overlay"></div>
</div>

<script>
  $('.base').hover(function(){
    $(this).removeAttr('style').children().removeAttr('id');
  }, function(){
    $(this).addBack();
  });
</script>

$('.base').hover(函数(){
$(this.removeAttr('style').children().removeAttr('id');
},函数(){
$(this.addBack();
});

您对的功能有一个错误的想法,无论如何,最好使用CSS类

比如:

  $('.base').hover(function(){
    $(this).addClass('myClass');
  }, function(){
    $(this).removeClass('myClass');
  });


删除ID的代码部分是不可逆的,因此我会在那里找到另一种行为/解决方案…

类似的方法应该可以工作:

<div class="base" style="background-image: url(img/3.jpg);">
  <div id="overlay"></div>
</div>

<script>
  $('.base').hover(function(){
    var $this = $(this);
    $this.data('style', $this.attr('style')).removeAttr('style').children().each(function(){
        var $this = $(this);
        $this.data('id', $this.attr('id')).removeAttr('id');
    });
  }, function(){
    var $this = $(this);
    $this.attr('style',$this.data('style')).removeData('style').children().each(function(){
        var $this = $(this);
        $this.attr('id', $this.data('id')).removeData('id');
    });
  });

</script>

$('.base').hover(函数(){
var$this=$(this);
$this.data('style',$this.attr('style')).removeAttr('style').children().each(function()){
var$this=$(this);
$this.data('id'),$this.attr('id')).removeAttr('id');
});
},函数(){
var$this=$(this);
$this.attr('style'),$this.data('style')).removeData('style').children().each(function()){
var$this=$(this);
$this.attr('id'),$this.data('id')).removeData('id');
});
});
基本上,我们将属性值存储在jQuery数据数组中。我没有测试它,但它应该可以工作,我已经不止一次使用这种方法。。
更新了代码,下面是一个正在运行的

为什么要删除悬停时的id?您是否试图避免应用某些css?我正在删除id,因为它设置为“不显示”。当我删除它时,就会显示div的内容。我一直在隐藏这些元素,直到有人悬停在上面。我认为你是对的,我真的不理解addBack。在这种情况下,我需要使用样式元素来应用背景图片。因此,虽然我可以尝试为id css添加和删除类,但我仍然会面临样式attributethanks的问题,这太神奇了。它几乎完全符合我的要求。它隐藏并返回背景图像。id有一些问题。它似乎没有被删除,因为div的内容没有像以前那样显示。嗯,还是一样。我把整个项目都放在一个代码笔里——这是旧代码——这可能会让我更容易看到我想要实现的目标。顺便说一句,我非常感谢您的帮助。此更新的代码必须有效,您有一个JSFIDLE链接,可以在其中看到它的实际操作(它删除/恢复子节点上的ID)。。这里是链接:这里是你的项目分叉,应用了这个函数:我在codepen上分叉了代码,所以你可以看到旧版本和新版本。不知何故,id处理存在问题,因为删除id后,具有id的div内的内容不会显示。我只是稍微了解我们在做什么,所以我不明白为什么它不起作用