最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display href link into div - Stack Overflow

programmeradmin2浏览0评论

I want to display the href link in the <div id="display"></div> tag so when I press anything in the menu or in my list it'll just open in the div with display as its id. I have this menu like this done

<div class="menu">
    <a href="" onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>

and my JavaScript is like this

$('#display').html($('.menu a').html());

I don't know much about javascript, but I think the javascript code is actually wrong, I would appreciate is someone would help me.

I want to display the href link in the <div id="display"></div> tag so when I press anything in the menu or in my list it'll just open in the div with display as its id. I have this menu like this done

<div class="menu">
    <a href="http://stackoverflow." onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>

and my JavaScript is like this

$('#display').html($('.menu a').html());

I don't know much about javascript, but I think the javascript code is actually wrong, I would appreciate is someone would help me.

Share Improve this question edited Jul 25, 2019 at 20:15 Seth McClaine 10.1k7 gold badges42 silver badges67 bronze badges asked Oct 8, 2013 at 14:37 Ace MunimAce Munim 3254 silver badges18 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

I want to display the href

You need to fetch href property for that you can use .prop()

$('#display').html($('.menu a').prop('href'));

Demo

In case you mean retrieve the page and place it in the div:

// bind click event to all anchors within .menu
$('.menu a').click(function(e){
  // fetch the page using AJAX and place the results in #display
  $('#display').load(this.href);
  // prevent navigating away from the page (default action of anchor)
  e.preventDefault();
});

(Or maybe it's just me, but the question seems very hard to understand. :shrug:)

$('.menu a').on('click',function(e){
  e.preventDefault(); //this will keep your link from loading
  var href = $(e.currentTarget()).attr('href');
  $('#display').html(href);
});

We can use an iframe to display the link in the <a> tag.

Here's a fiddle

Here is my version...

HTML

<div class="menu">
  <a  id="xxx" href="http://stackoverflow." onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>   

JS

$(document).ready(function() {  
  var data = $("a#xxx").attr("href");
  $('#display').html(data);
});
发布评论

评论列表(0)

  1. 暂无评论