如何在一个html页面中单击div并让它在另一个html页面中创建一个div?

时间:2014-03-03 22:09:52

标签: javascript jquery html5 cordova

我在这里找到了这个代码,允许我通过点击它在不同的部分创建一个div元素,它在同一个html文件中正常工作,如下面的fidle所示

http://jsfiddle.net/fb7Tq/109/

我的问题是我想让这段代码在不同的html文件之间工作,我该怎么办呢?

在第一个html中我有初始div。

<div class="section1">
<div id="1" >1</div>
<div id="2" >2</div>
<div id="3" >3</div>
<div id="4" >4</div>
<div id="5" >5</div>
<div id="6" >6</div>
<div id="7" >7</div>
<div id="8" >8</div>

然后当点击这些div时,他们需要在下面这个div中出现一个不同的html文件。

<div class="section2"></div>

这是js

    function testclick() {
    var iLoc = $(this).attr("id");
    $('.section2').append(this);
    $('#'+iLoc).off('click', testclick).bind('click',testclick2) ;
}

function testclick2() {

    var iLoc = $(this).attr("id");
    $('.section1').append(this);
    this.off('click', testclick2).on('click',testclick) ;
}
$('.section1 div').on('click', testclick);
$('.section2 div').on('click', testclick2);

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

通过评论中的说法,您需要的是一个带有服务器端代码的Web应用程序。静态html文件将无法提供。最后,为了达到你想要的效果,你必须在用户点击你的一个div时发送请求(直接或通过AJAX),这样服务器就会以某种方式“保存”用户的偏好并显示相应地修改了页面。

这不是一个微不足道的问题,肯定是用几行javascript无法实现的。解释如何使用服务器端代码构建Web应用程序超出了StackOverflow中问题的范围。

编辑我已经阅读了您发布的其他一些评论。所以你想用PhoneGap创建一个应用程序。那么,在这种情况下,也许你可以使用localStorage做其他事情。

当ser点击div时,使用localStorage.setItem存储有关用户选择内容的信息。 LocalStorage是在会话之间存储对象的持久位置。然后,下次打开要显示div的页面时,只需读取存储在localStorage中的值(使用localStorage.getItem()),然后使用该值显示正确的DIV。

有关localStorage的更多信息,请访问MDN。我希望这能为你服务。只要考虑到当用户在不同的设备中打开应用程序时,将不会设置这些首选项。配置将存储在设备中。