如何使用 JavaScript 创建链接?
我有一个标题字符串和一个链接字符串。我不知道如何将两者放在一起以使用 JavaScript 在页面上创建链接。任何帮助表示赞赏。
我试图解决这个问题的原因是因为我有一个 RSS 源并且有一个标题和 URL 列表。我想将标题链接到 URL 以使该页面有用。
我正在使用 jQuery,但对它完全陌生,并且不知道它可以在这种情况下提供帮助。
I have a string for a title and a string for a link. I'm not sure how to put the two together to create a link on a page using JavaScript. Any help is appreciated.
The reason I'm trying to figure this out is because I have an RSS feed and have a list of titles ands URLs. I would like to link the titles to the URL to make the page useful.
I am using jQuery but am completely new to it and wasn't aware it could help in this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 JavaScript
或者,按照@travis的建议:
使用 JQuery
在上述所有示例中,您可以将锚点附加到任何元素,而不仅仅是“主体”,并且
desiredLink
是一个变量,用于保存锚点元素的地址指向,desiredText
是一个变量,用于保存将在锚元素中显示的文本。With JavaScript
or, as suggested by @travis :
With JQuery
In all the above examples you can append the anchor to any element, not just to the 'body', and
desiredLink
is a variable that holds the address that your anchor element points to, anddesiredText
is a variable that holds the text that will be displayed in the anchor element.使用 JavaScript 创建链接:
OR
OR
Create links using JavaScript:
OR
OR
有几种方法:
如果您想使用原始 Javascript(没有像 JQuery 这样的帮助程序),那么您可以执行以下操作:
另一种方法是将链接直接写入文档中:
There are a couple of ways:
If you want to use raw Javascript (without a helper like JQuery), then you could do something like:
The other method is to write the link directly into the document:
使用原始 JavaScript 动态创建超链接:
Dynamically create a hyperlink with raw JavaScript:
“锚对象”有自己的*(继承的)*属性,用于设置链接及其文本。所以就用它们吧。 .setAttribute 更通用,但通常不需要它。
a.title ="Blah"
也会做同样的事情并且更清晰!需要 .setAttribute 的情况是这样的:
var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")
保持协议打开。
考虑只使用 //example.com/path,而不是 http://example.com/path。
检查 example.com 是否可以通过 http: 以及 https: 访问,但 95% 的网站可以在这两种方式上运行。
OffTopic:这与在 JS 中创建链接无关
但也许很高兴知道:
有时就像在 chrome 开发控制台中,您可以使用
$("body")
而不是document.querySelector("body")
A_$ = document .querySelector
将在您第一次使用它时通过非法调用错误来“尊重”您的努力。这是因为赋值只是“抓取”.querySelector(对 class 方法的引用)。使用.bind(...
),您还将涉及上下文(这里是document
),并且您将获得一个可以工作的object方法正如您所期望的那样。The 'Anchor Object' has its own*(inherited)* properties for setting the link, its text. So just use them. .setAttribute is more general but you normally don't need it.
a.title ="Blah"
will do the same and is more clear!Well a situation that'll demand .setAttribute is this:
var myAttrib = "title"; a.setAttribute( myAttrib , "Blah")
Leave the protocol open.
Instead of http://example.com/path consider to just use //example.com/path.
Check if example.com can be accessed by http: as well as https: but 95 % of sites will work on both.
OffTopic: That's not really relevant about creating links in JS
but maybe good to know:
Well sometimes like in the chromes dev-console you can use
$("body")
instead ofdocument.querySelector("body")
A_$ = document.querySelector
will 'honor' your efforts with an Illegal invocation error the first time you use it. That's because the assignment just 'grabs' .querySelector (a ref to the class method). With.bind(...
you'll also involve the context (here it'sdocument
) and you get an object method that'll work as you might expect it.一种肮脏但快速的创建元素的方法:
A dirty but quick way to create elements:
将其粘贴到里面:
Click here
You paste this inside :
<A HREF = "index.html">Click here</A>