jquery:隐藏title属性但不删除它

lau*_*kok 15 jquery attributes title

我已经看到大多数人都会使用这个解决方案,在鼠标悬停时,我们将获取TITLE属性中的值,然后删除其值.在鼠标移出时,我们会重新启用它.

$(this).attr('title',''); 
Run Code Online (Sandbox Code Playgroud)

要么

$(this).removeAttr('title'); 
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以隐藏工具提示而不是删除title属性?

谢谢!

FRo*_*owe 33

不,你不能,因为浏览器将决定如何处理title属性.但是,您可以将其与节点一起保存以供以后参考(并可能还原标题):

$(this).data("title", $(this).attr("title")).removeAttr("title");
Run Code Online (Sandbox Code Playgroud)


小智 9

这有效.

    $(document).ready(function(){
        $( "a" )
        	.mouseenter(function() {	
        		var title = $(this).attr("title");
        		$(this).attr("tmp_title", title);
        		$(this).attr("title","");
        	})
        	.mouseleave(function() {
        		var title = $(this).attr("tmp_title");
        		$(this).attr("title", title);
        	})
        	.click(function() {	
        		var title = $(this).attr("tmp_title");
        		$(this).attr("title", title);
        	});
        });
      });
Run Code Online (Sandbox Code Playgroud)


Dan*_*ett 6

当鼠标悬停在元素上时,您可以将标题存储在其他位置.但是,您不必将其存储在DOM本身中; 你可以将它保存在JS var中:

(function(){
  var ttext;
  $(yourtargetselectorhere).hover(function(){
    ttext = $(this).attr('title');
    $(this).removeAttr('title');
  },
  function(){
    $(this).attr('title', ttext);
  });
}());
Run Code Online (Sandbox Code Playgroud)