JxBrowser supports web pages that use jQuery JavaScript library and allows executing JavaScript code that make calls to jQuery. For example:(JxBrowser支持使用jQuery JavaScript库的网页,并允许执行对jQuery进行调用的JavaScript代码。例如:)
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.LoadAdapter; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; /** * The sample demonstrates how to execute JQuery code on the loaded * web page using JxBrowser API. */ public class JQuerySample { public static void main(String[] args) { Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(browserView, BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.addLoadListener(new LoadAdapter() { @Override public void onFinishLoadingFrame(FinishLoadingEvent event) { if (event.isMainFrame()) { // Hide all paragraphs using JQuery event.getBrowser().executeJavaScript("$('p').hide();"); } } }); // Load local *.html file that uses local JQuery jquery.min.js file. Path // to the local jquery.min.js file must be relative. browser.loadURL("jquery.html"); } }
The index.html file has the following content:(index.html文件具有以下内容:)
<!DOCTYPE html> <html> <head> <script src="jquery.min.js"></script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
Please note that this example requires the jquery.min.js file is located in the same directory where the index.html file is located. You can download jquery.min.js file at http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js(请注意,此示例要求jquery.min.js文件位于index.html文件所在的目录中。您可以从http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js下载jquery.min.js文件)