加载外部.htm文件与JavaScript的一个div加载、文件、div、htm

2023-09-10 15:53:14 作者:孤街酒客

所以,我得到这个code

So I got this code

JavaScript的:

Javascript:

<script type="text/javascript">
$(function(){
    $('.ajax') .click(function(e){
        e.preventDefault()
        $('#content').load( 'file.htm' )
    })
})
</script>

HTML:

html:

<a href="file.htm" class="ajax">Link</a>

它完全在Firefox,但没有任何反应,当我点击链接在Chrome和IE浏览器只是打开的文件的新窗口。什么建议?

it works perfectly in firefox, but nothing happens when I click the link in chrome and IE simply opens a new window with the file. any advice?

推荐答案

我不是任何形式的codeR,而且我知道有不止一种方法,使这项工作。 这是为我工作我的情况。 我有一个工作网站,但与code / DIV的内容都在一个页面,很多我想清理一下。 我希望这可以帮助别人!

I am not a coder of any sort, and I know there is more than one way to make this work. This is what worked for me for MY situation. I had a working site but with A LOT of code / DIV content all in one page and I wanted to clean that up. I hope this Helps someone else!

我一直在寻找这种解决方案很长一段时间,我已经跨越了如何可以在不同的情况下,工作的许多例子中运行。 我的方案如下: 我有一个使用了一系列包含各种页面DIV标签的摄影网站这么说的网站。 这些都被设置为:     &LT; D​​IV ID =DIVID的风格=显示:无&GT;&LT; / DIV&GT; 在页面的头部下面的脚本:     

I have been searching for this solution for quite some time and I have run across many examples of how it can work in different instances. My scenario was as follows: I have a photography website that uses a series of DIV tags containing the various "pages" so to speak of the site. These were all set as: <div id="DivId" style="display:none"></div> The following script in the head of the page:


    
    $(document).ready(function(){
    $('a').click(function () {
    var divname= this.name;
    $("#"+divname).show("slow").siblings().hide("slow");
    });
});
</script>

并要求使用这样的锚链接:     &LT; A HREF =NAME =家&GT; HOME&LT; / A&GT; 凡名字被称为DIV的名称。 请注意,DIV将父容器DIV中被调用。 最后,最重要的是这个特定的问题和情况下,DIV都放置在页面上,如上图所示。 每个div内容的创建,就好像它是在DIV标签,但减去的打开和关闭DIV标签内,然后保存为一个单独的.txt文件,并要求放置,这是父页面的头:      SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js&GT; 和     

and called using the anchor links like this: <a href="" name="home">HOME</a> Where name was the name of the DIV to be called. Please note the DIV will be called inside the parent container DIV. Lastly and most importantly for this particular question and scenario, the DIV were all placed on the page as shown above. Each div content was created just as if it were within the DIV tags but minus the opening and closing DIV tags and then saved as a separate .txt file, and called by placing this is the head of the parent page: src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"> and


    
    $(document).ready(function() { // this runs as soon as the page is ready (DOM is loaded)
        $("#DivName") // selecting "div" (you can also select the element by its id or class like in css )
        .load("PathToFile.txt");// load in the file specified
        $("#AnotherDiv").load("AnotherFile.txt");// Additional DIV can be added to populate DIVs on th eparent page.
    });