在将元素属性设置为空字符串之前,该如何存储它以便以后使用? [英] How can I store an element attribute for later use before setting it to an empty string?

查看:138
本文介绍了在将元素属性设置为空字符串之前,该如何存储它以便以后使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JQuery代码:

I have the following JQuery code:

  $(targetSelector)
    .attr('data-disabled', 'yes')
    .attr('title', '')
    .addClass('disabled')
    .prop('disabled', true);

如何更改此代码,以便在title属性设置为''之前,获取标题的当前值并将其存储在'data-title'属性中.

How can I change this code so that before the title attribute is set to '' I take the current value of the title and store it in the 'data-title' attribute.

推荐答案

您可以这样做:

$(targetSelector).attr({
    'data-disabled': 'yes',
    'data-title': function() { return this.title }
    'title': ''
  }).addClass('disabled').prop('disabled', true);

其他答案相似,但是仅当this是元素时才访问this.title,这取决于上下文.上面的方法改用function属性返回标题.

The other answers are similar, however accessing this.title will only work if this is the element, and that depends on the context. The method above uses the function attribute to return the title instead.

这篇关于在将元素属性设置为空字符串之前,该如何存储它以便以后使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆