0

私のサイトのテストページでは、このテキストを変更するjsのビットは問題なく機能しますが、実際のサイトに持ち込むと、下部のリンクはどれも機能しません。

完全に実装されたアイデアは、ページの下部にナビゲーションバーを配置して、ページコンテンツのHTMLコードを生成することです。実際のコンテンツ自体は、必要になるまでjavascript文字列内に隠されています。次に、ページ上の所定の位置に配置されます。

<html>
<head>
<title>Javascript Test #9</title>
<script type="text/javascript">
    window.onload = function() {
        document.getElementById("workl").onclick=workf;
        document.getElementById("aboutl").onclick=aboutf;
        document.getElementById("contactl").onclick=contactf;
        document.getElementById("quote").onclick=quotationf;        
    }

    function workf(){
        document.getElementById("para").innerHTML="Primary Changed Paragraph Content";
        quotationf("changed quote content");
        return false;
    }
    function aboutf(){
        document.getElementById("para").innerHTML="Secondary Changed Paragraph Content<br><br><br>";
        quotationf("changed quote content");

        return false;
    }       
    function contactf(){
        document.getElementById("para").innerHTML="Tertiary Changed Paragraph Content";
        quotationf("changed quote content");

        return false;
    }
    function quotationf (input){
        document.getElementById("quote").innerHTML=input;
        return false;
    }

</script>
</head>
<body>

    <p id="quote"> quote content</p>
    <p id="para">Unchanged Paragraph Content</p>    <br>
<a id="workl" href="#"> Click here to change the paragraph content </a><br>
<a id="aboutl" href="#"> Click here to change the paragraph content </a><br>
<a id="contactl" href="#"> Click here to change the paragraph content </a>

</body>
</html>

動作しないコードはここにあります:http://theblankframe.com/tempmobile/index.html

動作テストはここにあります:http://theblankframe.com/tempmobile/test2.html

どんな助けでも素晴らしいでしょう。ありがとう!

4

4 に答える 4

0

生産現場で、エラーが表示されます

SCRIPT5007:プロパティ'onclick'の値を設定できません:オブジェクトがnullまたは未定義です

コード行で

document.getElementById("workl").onclick=workf;

テストサイトでは、IDworklを使用してAタグを定義します。

<a id="workl" href="#"> Click here to change the paragraph content </a>

ただし、本番サイトではそのようなタグを定義しません。

本番環境のJavaScriptは、存在しないIDのIDで要素を取得しようとし、次に、見つからなかったその要素のメソッドを呼び出します。

于 2012-08-13T18:15:26.670 に答える
0

問題はここにあります

window.onload = function() {
document.getElementById("workl").onclick=workf; // << error thrown here
document.getElementById("aboutl").onclick=aboutf; //<< aboutl doesnt exist too
document.getElementById("contactl").onclick=contactf;
document.getElementById("quotet").onclick=quotationf;
} 

id を持つ要素がないため、オブジェクトworklにハンドラーをアタッチするとエラーが発生し、スクリプトの残りの部分が失敗しますnulldocument.getElementById("workl").onclick=workf;

于 2012-08-13T18:23:30.000 に答える
0

@本番環境では、ID「workl」の要素はありません。

getElementById が有効なものを返すかどうかを確認することをお勧めします。

if(document.getElementById("workl")){
   document.getElementById("workl").onclick=workf;
}
于 2012-08-13T18:23:39.687 に答える
0

work1これが機能するには、実際のサイトにid を持つ要素が必要です。これは、コード

document.getElementById("workl").onclick=workf;

その要素を探していますが、見つかりません。

于 2012-08-13T18:16:45.077 に答える