javascript - 使用 jquery - 2 个不同的链接打开同一窗口但显示适当的内容?

标签 javascript jquery

我正在尝试弄清楚如何使用 jQuery 来实现这一点,我的页面 1 有 2 个链接,即链接 1 和链接 2,我想要实现的是,当我单击链接 1 时,会打开一个新的浏览器窗口显示链接 1 的内容,当我单击链接 2 时,会显示相同的窗口,但包含链接 2 的内容。

这是创建一个页面并隐藏两组内容然后在单击相应链接时显示内容的问题吗?

有没有人有类似的教程?

最佳答案

尝试一下...我使用链接的 rel 属性来指向内容,并使用这些链接上的类,这允许点击处理程序适用于任意数量的链接/窗口。

<div style="display:none" id="content1">This displays for link1</div>
<div style="display:none" id="content2">This displays for link2</div>
<a class="windowlink" rel="content1" href="#">Link 1</a><br>
<a class="windowlink" rel="content2" href="#">Link 2</a>

<script>
$(document).ready(function() {                       // Standard jQuery wrapper to ensure DOM/jQuery is loaded and ready for use.
 $('.windowlink').click(function() {                 // Assign a function to the Click handler of all elements with a class of 'windowlink'
   var html = $('#' + $(this).attr('rel')).html();   // Builds a jQuery ID selector using the rel attribute of the clicked link, grabs the html content of the div with that name.
                                                     // i.e. link 1 has a rel="content1" so this line will evaluate to var html = $('#content2').html();
   var newwindow = open('');                         //  Open a new window, store the handle
   newwindow.document.write(html);                   // Write the HTML to the new window
   newwindow.document.close();                       // Stop writing, finalize the new window document.
 });
});

工作中的 JSFiddle: http://jsfiddle.net/gfosco/txMKb/

关于javascript - 使用 jquery - 2 个不同的链接打开同一窗口但显示适当的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6216820/

相关文章:

javascript - 如何正确地将 CSS 文件包含到页面中

jquery - 如何检查元素是否可放置、可拖动或其他 'ble' ?

javascript - 使用循环创建一个div

javascript - 使用 Scrapy - JS 制作蜘蛛

JQuery - .data() 中的数据丢失

javascript - 禁用按钮,直到单击一个单选按钮

javascript - 我需要获取 Instagram 用户 ID# 以便在 Google 脚本中使用

jquery - 当像 jQuery 中的灯箱一样单击链接时,如何在动态生成的 iFrame 中加载页面?

javascript - 从另一个函数内部调用私有(private)函数

javascript - 将参数传递给参数