繁体   English   中英

在div中加载html,然后将其删除

[英]Load html in div and then remove it

我是Java语言的初学者,而且一段时间以来都在为一个问题而苦苦挣扎。 我想从另一个本地html文档中打开现有div的新内容。 所以在主要内容中,我有一个简单的项目列表,当我单击一个项目时,我希望它在与我的项目相同的div '#content'中打开。 为此,我使用代码:

$(document).ready(function() {
    $('a.th-link').click(function() {
        var url = $(this).attr('href');
        $('#content').html('<h4>Loading...</h4>').load(url+ '#mynewdiv');
        $.getScript('js/jquery.js'); 
        $.getScript('js/jquery.horizontal.scroll.js'); 
        $.getScript('js/jquery.mousewheel.js'); 
        $(url).remove();
        return false;
    });
 });

问题是,当我想返回带有项目'#content'的主div ,使用鼠标或浏览器上的后退按钮,停留在其他div '#mynewdiv'上或进入先前的开放站点时。 有人可以提供代码帮助吗?

我仍然在处理这个问题。 我找到了一个与我的相似的粗糙示例,并且已经对其进行了改造,使其可以按我的意愿工作,但仍然无法正常工作。 刷新我的问题。

我想在某些div中打开一个新的html页面,它将可以返回按钮。

我也尝试了benalman烧烤,但我不知道自己在做什么错。

这是我在做什么

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>TEST</title>
    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load("jquery", "1.4.1");
    </script>
    <script src="jquery.address-1.1.min.js"></script>


    <script type="text/javascript">
    /*<![CDATA[*/
    $("document").ready(function(){

        function loadURL(url) {
            console.log("loadURL: " + url);
            $("#area").load(url);
        }


        // Event handlers
        $.address.externalChange(function(event) {
console.log("init: " + $('[rel=address:' + event.value + ']').attr('href'));
$("#area").load($('[rel=address:' + event.value + ']').attr('href'));
}).change(function(event) {
console.log("change");
});

        $('a.test').click(function(){
            loadURL($(this).attr('href'));
        });

        });

    /*]]>*/
    </script>

<style type="text/css">
#area {border-style:solid;
border-width:5px;
height:300px;
width:100%;}
</style>

</head>
<body>
<p>Test</p>
    <div id="area">

    <a class='test' href="test1.html" rel="address:/test1">Test 1</a> <br />
    <a class='test' href="test2.html" rel="address:/test2">Test 2</a> <br /> <br /> <br />

</body>
</html>

感谢您的回答

.load()获取数据并将其显示在同一站点上。 浏览器永远不会更改页面。 因此,当您单击返回时,浏览器将转到上一个打开的站点。

您可以使用jQuery Address之类的插件来模拟所需的行为。

将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div>

[英]load HTML into <div>

暂无
暂无

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

相关问题 加载html或div,菜单 将 HTML 装入<div></div><div id="text_translate"><p><em><strong>小提琴</strong>: <a href="https://jsfiddle.net/x9ghz1ko/" rel="nofollow noreferrer">https://jsfiddle.net/x9ghz1ko/</a> (带评论。)</em></p><p> 我有一个<strong>HTML 文档</strong>,在这个文档里面,有<strong>两个部分</strong>: &lt;section class="desktop_only"&gt;和&lt;section class="mobile_only"&gt; - 在代码中,在这些部分之前,有一个<strong>脚本</strong>,这个脚本应该<strong>删除两个部分之一</strong>,但问题是:为此,我必须将脚本放在部分之后,但我不能这样做,因为部分内有更多脚本,我必须删除它们在他们跑之前。</p><p> 我的代码的简化版本:(……不工作。) </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script&gt; device = "desktop"; // This could be either: "desktop", or: "mobile" if(device === "desktop"){ document.querySelector(".mobile_only").remove(); // Not doing anything. } else { document.querySelector(".desktop_only").remove(); // Not doing anything. } &lt;/script&gt; &lt;section class="desktop_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("desktop_only") &lt;/script&gt; &lt;div&gt; desktop &lt;/div&gt; &lt;/section&gt; &lt;section class="mobile_only"&gt; &lt;script src="example.js"&gt;&lt;/script&gt; &lt;script&gt; console.log("mobile_only") &lt;/script&gt; &lt;div&gt; mobile (Delete this.) &lt;/div&gt; &lt;/section&gt;</pre></div></div><p></p><p> <strong>也许一种解决方案是:</strong>我有一个空的&lt;div&gt; ,称为".platform_dependent" ,我有两个外部文件,称为: desktop_only.something和mobile_only.something然后我将这些文件的内容加载到: platform_dependent.innerHTML ...但我不知道该怎么做?</p></div> 将html链接加载到div中 jQuery在加载时按ID删除div 从HTML删除div-jQuery 在后台线程上将HTML加载到Div中? 使用脚本在 DIV 上加载可见的 HTML 使用JavaScript将HTML加载到DIV中 使用$ .load替换div中的HTML jQuery将html文件加载到<div>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM