简体   繁体   English

如何使用jQuery打开一个新的HTML页面?

[英]How to open a new HTML page using jQuery?

So, I am using IBM Worklight where I have the main file called file1.html and then I created another html file called file2.html . 所以,我使用的IBM工作灯在那里我有主文件名为file1.html然后我创建了另一个HTML文件file2.html

I am trying to open file2 but no luck so far. 我试图打开file2但到目前为止没有运气。 I tried following pieces of code: 我尝试了以下代码:

  1. $(this).load("file2.html");

  2. $("div1").load("file2.html"); //div1 is the id for outer div of file1

  3. WL.App.openUrl("file2.html");

  4. window.openURL("file2.html");

And none of these worked! 这些都没有奏效! Any suggestions? 有什么建议?

use window.open("file2.html"); 使用window.open("file2.html"); to open on new window, 在新窗口打开,

or use window.location.href = "file2.html" to open on same window. 或使用window.location.href = "file2.html"在同一窗口中打开。

Use window.open("file2.html"); 使用window.open(“file2.html”);

Syntax 句法

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

Return value and parameters 返回值和参数

windowObjectReference 

A reference to the newly created window. 对新创建的窗口的引用。 If the call failed, it will be null. 如果调用失败,则为null。 The reference can be used to access properties and methods of the new window provided it complies with Same origin policy security requirements. 如果引用符合Same origin策略安全性要求,则该引用可用于访问新窗口的属性和方法。

strUrl 

The URL to be loaded in the newly opened window. 要在新打开的窗口中加载的URL。 strUrl can be an HTML document on the web, image file or any resource supported by the browser. strUrl可以是Web上的HTML文档,图像文件或浏览器支持的任何资源。

strWindowName 

A string name for the new window. 新窗口的字符串名称。 The name can be used as the target of links and forms using the target attribute of an <a> or <form> element. 该名称可以使用<a><form>元素的target属性作为链接和表单的目标。 The name should not contain any blank space. 名称不应包含任何空格。 Note that strWindowName does not specify the title of the new window. 请注意, strWindowName不指定新窗口的标题。

strWindowFeatures 

Optional parameter listing the features (size, position, scrollbars, etc.) of the new window. 可选参数,列出新窗口的功能(大小,位置,滚动条等)。 The string must not contain any blank space, each feature name and value must be separated by a comma. 该字符串不得包含任何空格,每个要素名称和值必须用逗号分隔。

If you want to use jQuery, the .load() function is the correct function you are after; 如果你想使用jQuery,.load()函数是你正在使用的正确函数;

But you are missing the # from the div1 id selector in the example 2) 但是你缺少示例2中div1 id选择器的#

This should work: 这应该工作:

$("#div1").load("file2.html");

You need to use ajax. 你需要使用ajax。

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

<code>
$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});
</code>

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

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