javascript - 如何使用javascript从字符串中获取 anchor 标记的href值

标签 javascript html

如何在没有Jquery的情况下使用纯javascript获取下面字符串内容中最后一个 anchor 标记的href值。

var contents = '<div id="content"><a href="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg"><img title="Ruffcoin-Made-In-Aba.jpg" class="alignnone size-full" alt="image" src="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg" /></a>↵↵↵↵Indigenous heavyweight, <a href="http://www.okhype.com/tag/ruffcoin">Ruffcoin</a> recently dropped a new album titled “Made In Aba“. I’ve been giving it a few spins, and I must say, the album is quite solid.↵↵[matrix]↵↵Two of my early faves are “Aba Made” and “Higher Higher which feature <a href="http://www.okhype.com/tag/sparkle">Sparkle</a>)”. You should check ’em out, then download on iTunes <a href="https://itunes.apple.com/us/album/made-in-aba/id1180901826?ign-mpt=uo%3D4"><strong>hereq</strong></a>.↵↵Cop below.↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3">DOWNLOAD Ruffcoin - Aba Made</a></strong>↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3">DOWNLOAD Ruffcoin ft Sparkle - Higher Higher</a></strong></div>';

这是我要获取的部分

href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"

最佳答案

如果你create an element并将 HTML 字符串插入其中,您可以使用常用的 DOM 方法 getElementsByTagName 或 querySelector/querySelectorAll 来操作 HTML:

var contents = '<div id="content"><a href="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg"><img title="Ruffcoin-Made-In-Aba.jpg" class="alignnone size-full" alt="image" src="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg" /></a>↵↵↵↵Indigenous heavyweight, <a href="http://www.okhype.com/tag/ruffcoin">Ruffcoin</a> recently dropped a new album titled “Made In Aba“. I’ve been giving it a few spins, and I must say, the album is quite solid.↵↵[matrix]↵↵Two of my early faves are “Aba Made” and “Higher Higher which feature <a href="http://www.okhype.com/tag/sparkle">Sparkle</a>)”. You should check ’em out, then download on iTunes <a href="https://itunes.apple.com/us/album/made-in-aba/id1180901826?ign-mpt=uo%3D4"><strong>hereq</strong></a>.↵↵Cop below.↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3">DOWNLOAD Ruffcoin - Aba Made</a></strong>↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3">DOWNLOAD Ruffcoin ft Sparkle - Higher Higher</a></strong></div>';

var snippet = document.createElement("div");
snippet.innerHTML=contents;
var links = snippet.getElementsByTagName("a"),
    lastURL = links[links.length-1].href; // or getAttribute("href")
console.log(lastURL);

关于javascript - 如何使用javascript从字符串中获取 anchor 标记的href值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400935/

相关文章:

javascript - Discord Webhook 消息无法发送

javascript - Shiny 的 ui.R 是否支持在每个带有 tabsetPanel 的 tabPanel 上包含 html 页面?

javascript - 启用询问使用 javascript/jquery 保存下载文件

javascript - 使用 SVG 和 Javascript 绘制 WindBarbs

html - 覆盖内容并使其响应

javascript - 避免在不使用框架/iframe 的情况下重新加载内容

css - 调整嵌入的 div

html - 将 “Position: fixed” div 设置为与父 div 相同的宽度(flexbox 元素)

javascript - 在 JavaScript/jQuery 中使用按钮单击动态添加 div

css - 固定 div 中的垂直居中