簡體   English   中英

是否可以在webBrowser控件中運行JavaScript而無需訪問特定頁面?

[英]Is it possible to run JavaScript within a webBrowser Control without accessing a specific page?

我正在構建一個網頁,作為我的應用程序的輸出。 這意味着我直接在編輯文檔文本,而不是將控件指向文件。 我有以下代碼:

<html>
<head>
<style type = "text/css"> 
.circle {
    position:relative;
    moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    -khtml-border-radius: 10px;
    border-radius: 10px;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    border: 1px solid #000;

    height: 10px;
    width: 10px;
    background-color:#33FF00;
}
</style>

<script type="text/javascript">
var step = 0;
var color= '#0000FF';
function timer()
{
    var t=setTimeout("switchColor()",125);
}
function switchColor()
{
    if (step == 0) {color='#33FF00';}
    if (step == 1) {color='#33FF00';}
    if (step == 2) {color='#22AA55';}
    if (step == 3) {color='#1155AA';}
    if (step == 4) {color='#0000FF';}
    if (step == 5) {color='#0000FF';}
    if (step == 6) {color='#1155AA';}
    if (step == 7) {color='#22AA55';}
    step = step+1;
    if (step > 7) { step = 0;}

    var elements = document.getElementsByClassName('circle')
    for (var i = 0;i <elements.length;i++)
    {
        elements[i].style.backgroundColor=color;
    }

    timer()
}
</script>

</head>
<body onload="timer()" >
<div id="test" class="circle1"></div>
<div class="circle"></div><div class="circle"></div>
<br>
<br>
</body>
</html>

然后,通過使用stringBuilder將該代碼設置為webBrowser控件的documentText,將每行添加到StringBuilder.AppendLine()函數中,然后將整個stringBuilder轉換為字符串。

我收到以下錯誤消息:不支持getElementsByClassName函數,但沒有任何反應。 html本身可以完美運行。

WebBrowser控件可以實時運行javascript,但與Internet Explorer具有相同的限制。 控件中使用的所有命令都必須能夠在關聯計算機上的Internet Explorer中運行。

作為您的答案,我建議您使用jQuery。 它可以找到具有特定類並且在IE中效果很好的元素。 具體看一下類選擇器。

www.jquery.com

http://api.jquery.com/class-selector/

您將需要使用getElementsByTagName('*'),遍歷列表,並驗證每個項目是否具有所需的className。

暫無
暫無

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

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