在运行时设置href属性 [英] Setting href attribute at runtime

查看:114
本文介绍了在运行时设置href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行时使用< a> 标签设置 href 属性的最佳方式是使用jQuery?

What is the best way to set the href attribute of the <a> tag at run time using jQuery?

另外,如何获取属性的 href 属性的值, < a>使用jQuery的标签?

Also, how do you get the value of the href attribute of the <a> tag using jQuery?

推荐答案

获取或设置HTML元素的属性,您可以在jQuery中使用 element.attr() 功能。

To get or set an attribute of an HTML element, you can use the element.attr() function in jQuery.

要获取 href 属性,请使用以下代码:

To get the href attribute, use the following code:

var a_href = $('selector').attr('href');

要设置 href 属性,请使用以下代码:

To set the href attribute, use the following code:

$('selector').attr('href','http://example.com');

在这两种情况下,请使用适当的选择器。如果已经为anchor元素设置了类,请使用'。class-name',并且如果已为锚元素设置了id,请使用 '#element-id'

In both cases, please use the appropriate selector. If you have set the class for the anchor element, use '.class-name' and if you have set the id for the anchor element, use '#element-id'.

这篇关于在运行时设置href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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