简体   繁体   English

在新标签页中打开动态内容

[英]Open in new tab with dynamic content

for this example I'm going to use a property site. 在此示例中,我将使用物业网站。 If at the top of the site I had the main viewed property ad, and below a whole lot of small square divs with the other ad's. 如果在该网站的顶部,我有主观看的房地产广告,而在其他小广告的下面有很多小方块div。 Now when you click on one of the smaller ads it swaps place with the main ad (dynamic content change using JavaScript, all ad data exists in JavaScript multidimensional array and is loaded from sql via php via ajax on page load. 现在,当您点击其中一个较小的广告时,它会与主要广告交换位置(使用JavaScript进行动态内容更改,所有广告数据都存在于JavaScript多维数组中,并在页面加载时通过php通过ajax从sql加载到sql中。

Now when a user right clicks on one of these smaller ads and want to open it in a new tab (as the main ad) so all I need is to load the same page in the new tab with a ?id='id of ad' argument to do so. 现在,当用户右键单击这些较小的广告之一并希望在新标签页中打开它(作为主广告)时,我需要的是在新标签页中将同一页面加载为广告的“ id ='id”这样做的理由。

so how do it so the ad has an onlick (which runs javascript to switch it with the main ad) and have a right click open in new tab which loads same page with the argument in the new tab? 那么该广告如何具有onlick(运行javascript以与主广告进行切换)并在新标签页中右键单击打开,该标签会在新标签页中加载带有参数的同一页?

like if there was a jquery function for this would be amazing. 就像有一个jQuery函数一样,这将是惊人的。

You need to use a hashtag function and append a hash value onto the URL. 您需要使用井号标签函数,并将井号值附加到URL上。 So the square divs would actually be links like: 因此,平方div实际上是像这样的链接:

<a href="http://your-site.com/page#advert1">Ad 1</a>
<a href="http://your-site.com/page#advert2">Ad 2</a>
<!-- Ad3, Ad4 etc. -->

Here's an example of the jQuery you could use to read the hashtag: 这是您可以用来读取井号的jQuery的示例:

// CHECK IF A HASHTAG EXISTS IN THE URL
if(window.location.hash.length > 2) {
    // GET THE HASHTAG NAME
    var advert = window.location.hash.replace('#', '').replace(/\/$/, '').substr(1);

    // THEN CALL FUNCTION TO GET AD CONTENT FROM YOUR ARRAY USING THIS INFO
}

Alternatively you could use a plugin for this like jQuery Address which is made for this exact thing and has a load more features including the capture of hashchange events and click tracking for Google Analytics. 或者,您可以为此使用一个插件,例如jQuery Address ,它是专门针对此事物而制作的,并且具有更多功能,包括捕获hashchange事件和Google Analytics(分析)的点击跟踪。

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

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