Xav*_*ier 117 html javascript jquery dom hyperlink
我有一个标题字符串和一个链接字符串.我不知道如何将两者结合在一起使用Javascript在页面上创建链接.任何帮助表示赞赏.
编辑1:为问题添加更多细节.我试图解决这个问题的原因是因为我有一个RSS提要并且有一个标题和URL列表.我想将标题链接到URL以使页面有用.
编辑2:我正在使用jQuery,但它是全新的,并不知道它可以帮助在这种情况下.
Jar*_*ish 216
<html>
<head></head>
<body>
<script>
var a = document.createElement('a');
var linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.href = "http://example.com";
document.body.appendChild(a);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
gio*_*_13 57
var a = document.createElement('a');
a.setAttribute('href',desiredLink);
a.innerHTML = desiredText;
// apend the anchor to the body
// of course you can append it almost to any other dom element
document.getElementsByTagName('body')[0].appendChild(a);
Run Code Online (Sandbox Code Playgroud)document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';
Run Code Online (Sandbox Code Playgroud)
document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);
Run Code Online (Sandbox Code Playgroud)<script type="text/javascript">
//note that this case can be used only inside the "body" element
document.write('<a href="'+desiredLink+'">'+desiredText+'</a>');
</script>
Run Code Online (Sandbox Code Playgroud)$('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
Run Code Online (Sandbox Code Playgroud)$('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
Run Code Online (Sandbox Code Playgroud)var a = $('<a />');
a.attr('href',desiredLink);
a.text(desiredText);
$('body').append(a);
Run Code Online (Sandbox Code Playgroud)在上面的所有示例中,您可以将锚点附加到任何元素,而不仅仅是"body",并且desiredLink
是一个包含锚元素指向的地址desiredText
的变量,并且是一个包含将在其中显示的文本的变量.锚元素.
Nav*_*eed 15
使用JavaScript创建链接:
<script language="javascript">
<!--
document.write("<a href=\"www.example.com\">");
document.write("Your Title");
document.write("</a>");
//-->
</script>
Run Code Online (Sandbox Code Playgroud)
要么
<script type="text/javascript">
document.write('Your Title'.link('http://www.example.com'));
</script>
Run Code Online (Sandbox Code Playgroud)
要么
<script type="text/javascript">
newlink = document.createElement('a');
newlink.innerHTML = 'Google';
newlink.setAttribute('title', 'Google');
newlink.setAttribute('href', 'http://google.com');
document.body.appendChild(newlink);
</script>
Run Code Online (Sandbox Code Playgroud)
小智 11
有两种方法:
如果你想使用原始的Javascript(没有像JQuery这样的帮助器),那么你可以这样做:
var link = "http://google.com";
var element = document.createElement("a");
element.setAttribute("href", link);
element.innerHTML = "your text";
// and append it to where you'd like it to go:
document.body.appendChild(element);
Run Code Online (Sandbox Code Playgroud)
另一种方法是将链接直接写入文档:
document.write("<a href='" + link + "'>" + text + "</a>");
Run Code Online (Sandbox Code Playgroud)
使用原始 JavaScript 动态创建超链接:
var anchorElem = document.createElement('a');
anchorElem.setAttribute("href", yourLink);
anchorElem.innerHTML = yourLinkText;
document.body.appendChild(anchorElem); // append your new link to the body
Run Code Online (Sandbox Code Playgroud)
<script>
_$ = document.querySelector .bind(document) ;
var AppendLinkHere = _$("body") // <- put in here some CSS selector that'll be more to your needs
var a = document.createElement( 'a' )
a.text = "Download example"
a.href = "//bit\.do/DeezerDL"
AppendLinkHere.appendChild( a )
// a.title = 'Well well ...
a.setAttribute( 'title',
'Well well that\'s a link'
);
</script>
Run Code Online (Sandbox Code Playgroud)
“锚对象”有自己的*(继承的)*属性,用于设置链接及其文本。所以就用它们吧。.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 (对类方法的引用 )。您还将涉及上下文(这里是),并且您将获得一个可以按照您的预期工作的对象方法。.bind(...
document
归档时间: |
|
查看次数: |
346853 次 |
最近记录: |