如何使用 jQuery 更改元素的标题属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/987967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change an element's title attribute using jQuery
提问by Brian
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?
我有一个表单输入元素,想更改其标题属性。这必须像馅饼一样简单,但由于某种原因,我找不到如何做到这一点。这是如何完成的,我应该在哪里以及如何搜索如何做到这一点?
回答by C???
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 propertiesinitialized 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 attributescollection.
在我们编写任何代码之前,让我们讨论一下特性和属性之间的区别。属性是应用于HTML 标记中元素的设置;然后浏览器解析标记并创建各种类型的 DOM 对象,这些对象包含用属性值初始化的属性。在 DOM 对象上,例如 simple HTMLElement
,您几乎总是希望使用它的properties,而不是它的attributes集合。
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 propertyon many HTMLElement
s, we should take advantage of it.
当前的最佳实践是避免使用属性,除非它们是自定义的或没有等效的属性来补充它。由于title
确实作为许多s上的读/写属性存在HTMLElement
,我们应该利用它。
You can read more about the difference between attributes and properties hereor here.
With this in mind, let's manipulate that title
...
考虑到这一点,让我们操纵title
...
Get or Set an element's title
property withoutjQuery
不使用jQuery获取或设置元素的title
属性
Since title
is a public property, you can set it on any DOM element that supports it with plain JavaScript:
由于title
是一个公共属性,您可以在任何支持它的 DOM 元素上使用纯 JavaScript 设置它:
document.getElementById('yourElementId').title = 'your new title';
Retrieval is almost identical; nothing special here:
检索几乎相同;这里没什么特别的:
var elementTitle = document.getElementById('yourElementId').title;
This will be the fastest way of changing the title if you're an optimization nut, but since you wanted jQuery involved:
如果您是一个优化狂,这将是更改标题的最快方法,但由于您希望使用 jQuery:
Get or Set an element's title
propertywith jQuery (v1.6+)
使用 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:
jQuery 在 v1.6 中引入了一种新方法来获取和设置属性。要title
在元素上设置属性,请使用:
$('#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');
Check out the prop()
API documentationfor jQuery.
查看jQuery的prop()
API 文档。
If you reallydon't want to use properties, or you're using a version of jQuery prior to v1.6, then you should read on:
如果您真的不想使用属性,或者您使用的是 v1.6 之前的 jQuery 版本,那么您应该继续阅读:
Get or Set an element's title
attributewith jQuery (versions <1.6)
使用 jQuery获取或设置元素的title
属性(版本 <1.6)
You can change the title
attributewith the following code:
您可以使用以下代码更改title
属性:
$('#yourElementId').attr('title', 'your new title');
Or retrieve it with:
或使用以下方法检索它:
var elementTitle = $('#yourElementId').attr('title');
Check out the attr()
API documentationfor jQuery.
查看jQuery的attr()
API 文档。
回答by Igor L.
In jquery ui modal dialogs you need to use this construct:
在 jquery ui 模态对话框中,您需要使用此构造:
$( "#my_dialog" ).dialog( "option", "title", "my new title" );
回答by womp
I beleive
我相信
$("#myElement").attr("title", "new title value")
or
或者
$("#myElement").prop("title", "new title value")
should do the trick...
应该做的伎俩...
I think you can find all the core functions in the jQuery Docs, although I hate the formatting.
我认为您可以在jQuery Docs 中找到所有核心功能,尽管我讨厌格式。
回答by Greg Campbell
Another option, if you prefer, would be to get the DOM element from the jQuery object and use standard DOM accessors on it:
如果您愿意,另一种选择是从 jQuery 对象获取 DOM 元素并在其上使用标准 DOM 访问器:
$("#myElement")[0].title = "new title value";
The "jQuery way", as mentioned by others, is to use the attr() method. See the API documentation for attr() here.
正如其他人所提到的,“jQuery 方式”是使用 attr() 方法。请在此处查看 attr() 的 API 文档。
回答by Erdi Erta?
jqueryTitle({
title: 'New Title'
});
for first title:
对于第一个标题:
jqueryTitle('destroy');
回答by R Singh
If you are creating a div
and trying to add a title
to it.
如果您正在创建一个div
并尝试向其中添加一个title
。
Try
尝试
var myDiv= document.createElement("div");
myDiv.setAttribute('title','mytitle');