简体   繁体   English

如何在标签内获取内部href?

[英]How to get inner href inside a tag?

The whole element is like below :整个元素如下所示:

<h2 class="font120 mt0 mb10 mobfont110 lineheight20 moblineheight15"><a href="https://demosite.com">demo text</a></h2>

I have run this below command :我在下面的命令中运行了这个:

const y = document.getElementsByClassName("font120 mt0 mb10 mobfont110 lineheight20 moblineheight15")[0].innerHTML;
console.log(y);

And got the below result from it.并从中得到以下结果。

<a href="https://demosite.com">demo text</a>

Now I want to get href URL link, and then click on it.现在我想获取href URL链接,然后点击它。 How can I do it using JavaScript?我怎样才能使用 JavaScript 做到这一点? Also In addition to that, I want to add if else statement after this which will check if href URL changes in every 5 minutes , if it changes then it should click on href URL link otherwise it should just refresh the page.此外,我想在此之后添加 if else 语句,它将检查 href URL 是否每 5 分钟更改一次,如果更改,则应单击 href URL 链接,否则应刷新页面。

I think this comes close to what you want.我认为这接近你想要的。 I've tested it locally and works.我已经在本地对其进行了测试并且可以正常工作。

  1. There are a series of anchors inside containers.容器内有一系列锚。 Each anchor has a click event attached to it.每个锚点都有一个附加的点击事件。

  2. The initial href value is set to the first anchor's href attribute.初始 href 值设置为第一个锚点的 href 属性。

  3. The code then watches for any new clicks on the anchors.然后代码会监视锚点上的任何新点击。 If there is one a timer starts.如果有一个计时器启动。 If the new href is the same the saved one after five seconds the page reloads.如果新的href与五秒钟后保存的href相同,则页面重新加载。 If it's not the same the new page is loaded.如果不同,则加载页面。

  4. On each new click the timer resets.在每次新单击时,计时器都会重置。

 // Pick up the anchors with `querySelectorAll` const anchors = document.querySelectorAll('.demo a'); // Assign a handler to them for when the click // event is triggered. (Note `handleClick` returns // a new function that is actually assigned to the // listener because I wanted to use prevent any unnecessary // global variables, and you can do that with closure anchors.forEach(anchor => { anchor.addEventListener('click', handleClick()); }); function handleClick() { // Initialise the current href, and // the timer let current = anchors[0].href; let timer; // Return the function the listener will // be using return function (e) { // Stop the anchor from doing anything e.preventDefault(); // Get the new href from the clicked element const { href } = e.target; // Clear any timers clearTimeout(timer); // And set a new timer. If the current // href doesn't match the one on the clicked // element assign the page to a new location, // otherwise reload the current page. // This time is set for five seconds for testing // but you change that to five minutes (60000 x 5) timer = setTimeout(() => { if (current !== href) { // location.assign(href); console.log(`Redirecting to ${href}`); } else { // location.reload(); console.log(`Refreshing ${current}`); } current = href; }, 5000); } }
 <div class="demo"> <a href="https://demosite1.com">demo text 1</a> </div> <div class="demo"> <a href="https://demosite2.com">demo text 2</a> </div> <div class="demo"> <a href="https://demosite3.com">demo text 3</a> </div>

Additional documentation附加文件

All you need to do is look for the a element using the CSS selector h2 a and then get its href attribute:您需要做的就是使用 CSS 选择器h2 a查找a元素,然后获取其href属性:

 console.log(document.querySelector("h2 a").href)
 <h2 class="font120 mt0 mb10 mobfont110 lineheight20 moblineheight15"><a href="https://demosite.com">demo text</a></h2>

In order to set up a reload of the page you could set up a为了设置页面的重新加载,您可以设置一个

setTimeout("location.reload()",5*60*1000)

This will reload the current page after five minutes and will continue to do so, as the reloaded page will contain the same (unfired) setTimeout function.这将在五分钟后重新加载当前页面并将继续这样做,因为重新加载的页面将包含相同的(未触发的) setTimeout函数。

In order to check, whether the href value has changed from one to the next reloaded version you would need to store the value in localstorage and compare it every time after the page loads with the current href value.为了检查href值是否已从一个更改为下一个重新加载的版本,您需要将该值存储在localstorage中,并在每次页面加载后将其与当前的href值进行比较。

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

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