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

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

问题描述

我有一个表单输入元素,想更改其标题属性.这必须像馅饼一样简单,但由于某种原因,我找不到如何做到这一点.这是如何完成的,我应该在哪里以及如何搜索如何做到这一点?

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?

推荐答案

在编写任何代码之前,让我们先讨论一下属性和属性之间的区别.属性是您应用于 HTML 标记 中的元素的设置;然后浏览器解析标记并创建各种类型的 DOM 对象,这些对象包含用属性值初始化的 properties.在 DOM 对象上,例如简单的 HTMLElement,您几乎总是希望使用它的 properties,而不是它的 attributes 集合.

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.

当前的最佳做法是避免使用属性,除非它们是自定义的或没有等效的属性来补充它.由于 title 确实作为许多 HTMLElement 的读/写 属性 存在,我们应该利用它.

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.

您可以在此处此处.

考虑到这一点,让我们操作 title...

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

由于 title 是一个公共属性,你可以在任何支持它的 DOM 元素上使用纯 JavaScript 设置它:

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 中引入了一个新的方法来获取和设置属性.要在元素上设置 title 属性,请使用:

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');

查看 prop() 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:

您可以使用以下代码更改title 属性:

You can change the title attribute with the following code:

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

或者使用:

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

查看 attr() API 文档 jQuery.

Check out the attr() API documentation for jQuery.

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

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