简体   繁体   English

HTML和jQuery获取href

[英]HTML & jQuery get href

I'm new to this website so I'm sorry if I do something wrong, please tell me if I do so. 我是该网站的新手,对不起,如果我做错了什么,请告诉我是否这样做。

Problem: I'm trying to get a href from the current page I'm on with a button like "Get Href" and when it manages to get a href it will display text Href gotten or if it fails it says Href not found . 问题:我正在尝试使用诸如“获取Href”之类的按钮从当前页面中获取href,当它设法获取href时,它会显示文本Href或如果失败则显示未找到Href If it does find it a new button appears which says Goto Href and that takes the user to the href. 如果找到它,则会出现一个新按钮,显示Goto Href并将用户带到href。 I've been searching the whole week for a solution but so far I have no luck. 我整周都在寻找解决方案,但到目前为止我还没有运气。 Also this is a chrome extension. 这也是chrome扩展名。

Useful Links Found So Far: 到目前为止找到的有用链接:

I am new to HTML & jQuery & CSS & Javascript so I apologize for my lack of knowledge in advance. 我是HTML,jQuery,CSS和Java语言的新手,因此我对自己的知识不足深表歉意。 If you are able please explain your answers in detail. 如果可以,请详细解释您的答案。 If answer is found please comment the functions out so I could manually research them further. 如果找到答案,请注释掉这些功能,以便我进一步进行手动研究。

I don't know where to start, what I have so far is what the Getting Started: Building a Chrome Extension has. 我不知道从哪里开始,到目前为止,我所掌握的只是入门:构建Chrome扩展程序

----UPDATE .1---- ----更新.1 ----

I thank you for your replies and what I've gathered so far from them is this code 感谢您的答复,到目前为止,我收到的这些代码是

<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($('a').attr('href'));
  });
});
</script>
</head>
<body>

<button>Find Href</button>
</body>

but nothing seems to happen. 但似乎什么也没发生。 Also instead of alert($('a').attr('href')); 也可以代替alert($('a').attr('href')); I'm trying to put $('a[href^="magnet"]') This code is gathered from different websites so I don't know if it is legit (still working) or not. 我正在尝试放入$('a[href^="magnet"]')此代码是从其他网站收集的,所以我不知道它是否合法(仍在工作)。

try this javascript: 试试这个javascript:

document.location.href

I type it in the console and it returns: 我在控制台中键入它并返回:

http://stackoverflow.com/questions/24225281/html-jquery-get-href 

, which is current link. ,这是当前链接。

Before you start, you should read some javascript tutorials. 在开始之前,您应该阅读一些javascript教程。 Go search the internet for "javascript tutorial" (...please... do it). 在Internet上搜索“ javascript教程”(...请执行此操作)。 If you fail on this task, you will fail on others. 如果您在此任务上失败,您将在其他任务上失败。 This is not helpfull for you or others. 这对您或其他人没有帮助。

What do you want to do with "href" and why do you search only for chrome answers rather than search for a javascript answer. 您想使用“ href”做什么,为什么只搜索chrome答案而不搜索javascript答案。

Anyway, if you open the console you can test this by yourself. 无论如何,如果您打开控制台,则可以自己进行测试。

alert(location) <-- shows you the current location of the browser

or every href of every a tag (in jquery). 或每个标签的每个href(在jquery中)。

alert($('a').attr('href')); <-- a lot of links ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM