如何使用jQuery更改元素的title属性 [英] How to change an element's title attribute using jQuery

查看:124
本文介绍了如何使用jQuery更改元素的title属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单输入元素,想要改变其标题属性。这必须是易如反掌,但由于某些原因,我无法找到如何做到这一点。这是怎么做的,以及在哪里以及如何我应该如何做到这一点搜索?

I have an form input element and want to change its title attribute. This has to be easy as pie, but for some reason I cannot find how to do this. How is this done, and where and how should I be searching on how to do this?

推荐答案

在我们写任何code,让我们讨论属性和特性之间的差异。属性是你申请的元素在你的 HTML标记的设置;然后浏览器解析标记并创建包含的属性的初始化属性的值不同类型的DOM对象。在DOM对象,如一个简单的 HTML元素,你几乎总是要与它的属性来工作的,而不是它的属性收藏。

Before we write any code, let's discuss the difference between attributes and properties. Attributes are the settings you apply to elements in your HTML markup; the browser then parses the markup and creates DOM objects of various types that contain properties initialized with the values of the attributes. On DOM objects, such as a simple HTMLElement, you almost always want to be working with its properties, not its attributes collection.

目前最好的做法是避免使用属性,除非它们是自定义或者没有相等的属性来补充它。由于标题确实存在,作为一个读/写的属性许多 HTML元素 S,我们应该好好利用这个优势。

The current best practice is to avoid working with attributes unless they are custom or there is no equivalent property to supplement it. Since title does indeed exist as a read/write property on many HTMLElements, we should take advantage of it.

您可以在此处详细了解属性和属性这里

You can read more about the difference between attributes and properties here or here.

考虑到这一点,让我们操纵的标题 ...

With this in mind, let's manipulate that title...

由于标题是一个公共属性,你可以将它与普通的JavaScript支持任何DOM元素上:

Since title is a public property, you can set it on any DOM element that supports it with plain JavaScript:

document.getElementById('yourElementId').title = 'your new title';

检索几乎是相同的;这里没有什么特别:

Retrieval is almost identical; nothing special here:

var elementTitle = document.getElementById('yourElementId').title;

这将是如果你是一个优化螺母更改标题的最快的途径,但既然你想参与jQuery的:

This will be the fastest way of changing the title if you're an optimization nut, but since you wanted jQuery involved:

jQuery的介绍了V1.6的新方法来获取和设置属性。要设置标题属性的元素,使用:

jQuery introduced a new method in v1.6 to get and set properties. To set the title property on an element, use:

$('#yourElementId').prop('title', 'your new title');

如果您想获取冠军,省略第二个参数,获取返回值:

If you'd like to retrieve the title, omit the second parameter and capture the return value:

var elementTitle = $('#yourElementId').prop('title');

查看 道具() API文档 jQuery的。

Check out the prop() API documentation for jQuery.

如果您的真正的不想使用属性,或者您正在使用V1.6之前版本的jQuery,那么你应该阅读:

If you really don't want to use properties, or you're using a version of jQuery prior to v1.6, then you should read on:

您可以修改标题属性的有以下code:

You can change the title attribute with the following code:

$('#yourElementId').attr('title', 'your new title');

或用检索:

var elementTitle = $('#yourElementId').attr('title');

查看 ATTR() jQuery的API文档

Check out the attr() API documentation for jQuery.

这篇关于如何使用jQuery更改元素的title属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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