簡體   English   中英

在網頁中插入JavaScript

[英]Inserting javascript in web page

嗨,首先,我有一個問題,如何將javascript插入已經具有腳本標簽的網頁中例如:在網頁中,有javascript代碼

function win(){
alert("your score is 500"); }

我想插入alert(“ Winner!”); 高於警告(“您的分數是500”); 因此代碼將類似於網頁中的代碼:

function win(){
alert("Winner!");
alert("your score is 500"); }

我已經嘗試過使用此代碼

WebBrowser1.Document.Body.AppendChild(WebBrowser1.Document.CreateElement("script")) 
WebBrowser1.Document.InvokeScript("win", New [Object]() {"(function() { alert('Winner!'); alert('your score is 500');})()"})

但它不起作用,可以幫我嗎? 謝謝

為什么要創建新的腳本標簽並附加到head元素(如果已存在)?

您可以嘗試:

HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("script");
IHTMLScriptElement element = (IHTMLScriptElement)elems[0];
element.text = "function win() { alert('Winner!');alert('your score is 500');}";
webBrowser1.Document.InvokeScript("win");

注意:尚未實際測試代碼,希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM