Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

javascript - 在运行时设置href属性(Setting href attribute at runtime)

What is the best way to set the href attribute of the <a> tag at run time using jQuery?(使用jQuery在运行时设置<a>标记的href属性的最佳方法是什么?)

Also, how do you get the value of the href attribute of the <a> tag using jQuery?(另外,如何使用jQuery获取<a>标签的href属性的值?)   ask by gautamlakum translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get or set an attribute of an HTML element, you can use the element.attr() function in jQuery.(要获取或设置HTML元素的属性,可以在jQuery中使用element.attr()函数。)

To get the href attribute, use the following code:(要获取href属性,请使用以下代码:) var a_href = $('selector').attr('href'); To set the href attribute, use the following code:(要设置href属性,请使用以下代码:) $('selector').attr('href','http://example.com'); 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' .(如果已为anchor元素设置类,请使用'.class-name' ,如果已设置anchor元素的id,请使用'#element-id' 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...