簡體   English   中英

顯示href鏈接到div

[英]Display href link into div

我想在<div id="display"></div>標記中顯示href鏈接,因此當我按菜單或列表中的任何內容時,它將在div中以displayid打開。 我有這樣的菜單

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

我的JavaScript是這樣的

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

我對javascript不太了解,但是我認為javascript代碼實際上是錯誤的,我希望有人會幫助我。

我想顯示href

您需要獲取href屬性,以便可以使用.prop()

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

演示版

萬一您要檢索頁面並將其放在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();
});

(或者也許只是我一個人,但問題似乎很難理解。

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

我們可以使用iframe在<a>標簽中顯示鏈接。

這是一個小提琴

這是我的版本...

的HTML

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

JS

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM