我正在尝试弄清楚如何使用 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/