简体   繁体   English

jQuery replaceWith:根据屏幕分辨率将一个php文件替换为另一个php文件

[英]Jquery replaceWith:replace one php file with another php file based on screen resolution

I need to be able to replace a php file with another php file based on screen resolution. 我需要能够根据屏幕分辨率将php文件替换为另一个php文件。 This is what I have so far: 这是我到目前为止的内容:

<script type="text/javascript">
function adjustStyle(width) {
width = parseInt(width);
if (width = 1920) {
$('book.php').replaceWith('book2.php');
}
}
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});
</script>

which obviously isn't working-- any ideas? 哪个显然行不通-任何想法? Thank you in advance for any help received. 预先感谢您提供的任何帮助。

UPDATE 更新

Is this at all close (to replace the book.php line)? 这是否完全关闭(以替换book.php行)?

 { $("a[href*='book.php']").replaceWith('href', 'book2.php'); }; 

Second Update to reflect input gathered here 第二次更新以反映此处收集的输入

function adjustStyle(width) {
width = parseInt(width);
if (width == 1920) {
$('#bookinfo').replaceWith(['book2.php']);
$.ajax({
 url: "book2.php",
 }).success(function ( data ) {
  $('#bookinfo').replaceWith(data);
});
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});
}
}

I have not seen the use of replaceWith in the context you put it in. Interpreting that you want to exchange the content, you may want to do so my using the load() function of jQuery. 我没有看到在您放置的上下文中使用replaceWith。解释您要交换内容,您可能想使用jQuery的load()函数这样做。

    if(width == 1920){
            $("#myDiv").load("book1.php");
    } else {
            $("#myDiv").load("book2.php");
    }

Clicking on the button replaces the content of the div to book2.php. 单击该按钮会将div的内容替换为book2.php。

The first problem is I don't think that you are using the correct selectors. 第一个问题是我认为您使用的选择器不正确。 If you have the following container: 如果您具有以下容器:

<div id="bookContainer">Contents of book1.php</div>

The code to replace the contents of that container should be 替换该容器内容的代码应为

$('#bookContainer').replaceWith([contents of book2.php]);

In order to get [contents of book2.php] you will need to pull it in by ajax using the following code I have also included the line above so that the data from book2.php will be placed into the container: 为了获得[book2.php的内容],您需要使用以下代码通过ajax将其拉入,我也包括上面的行,以便将book2.php中的数据放入容器中:

$.ajax({
 url: "http://yoururl.com/book2.php",
 }).success(function ( data ) {
  $('#bookContainer').replaceWith(data);
});.

I haven't tested this so there might be an issue but this is the concept you need to accomplish this. 我尚未对此进行测试,因此可能存在问题,但这是您需要完成此操作的概念。

First off... using a conditional with a single = (equal sign) will cause the condition to always be true while setting the value of variable your checking to the value your checking against. 首先...使用带单个=(等号)的条件将条件设为true,同时将您要检查的变量的值设置为您要检查的值。

Your condition should look like the following... 您的情况应如下所示...

if (width == 1920) { // do something } if(width == 1920){//做某事}

Second, please refer to the jQuery documentation for how to replace the entire tag with a jquery object using replaceWith()... http://api.jquery.com/replaceWith/ 其次,请参考jQuery文档,了解如何使用replaceWith()将整个标签替换为jquery对象... http://api.jquery.com/replaceWith/

I would use a shorthand POST with http://api.jquery.com/jQuery.post/ since you don't have the object loaded yet... 我会在http://api.jquery.com/jQuery.post/中使用简写POST,因为您尚未加载对象...

In short, my code would look like the following using $.post instead of $.ajax assuming I had a tag with the id of "book" that originally has the contents of book.php and I wanted to replace it with the contents of book2.php... 简而言之,我的代码如下所示,使用$ .post代替$ .ajax,假设我有一个标签为“ book”的标签,该标签最初具有book.php的内容,而我想将其替换为book2.php ...

HTML 的HTML

<div id="book">*Contents of book.php*</div>

jQuery jQuery的

function onResize(width) {
    if (parseInt(width) >= 1920) {
        $.post('book2.php',function(html){
           $('#book').html(html).width(width);
        });        
    }
    else {
        $.post('book.php',function(html){
            $('#book').html(html).width(width);
        });
    }
}

Hope this helps. 希望这可以帮助。

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

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