在运行时使用< 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','example');在这两种情况下,请使用适当的选择器。如果已经为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属性
发布评论