2017-02-18 170 views
-3
<script> 
var a = false; 
function toggleBold(){ 
    a = !a; 
    if(a){ 
     document.getElementById("entertext").style.fontWeight=700; 
    } 
    else{ 
     document.getElementById("entertext").style.fontWeight=200; 
    } 
} 

var b = false; 
function toggleitalic(){ 
    b = !b; 
    if(b){ 
     document.getElementById("entertext").style.fontStyle="italic"; 
    } 
    else { 
     document.getElementById("entertext").style.fontStyle="normal"; 
    } 
} 

var c = false; 
function toggleline() { 
    c =!c; 
    if(c){    
     document.getElementById("entertext").style.textDecoration="underline"; 
    } 
    else { 
     document.getElementById("entertext").style.textDecoration="none"; 
    } 
} 


var r = false; 
function togglered() { 
    console.log("clicked"); 
    r=!r; 
    if(r){ 
     document.getElementById("entertext").style.color="red"; 
    } 
    else{ 
     document.getElementById("entertext").style.color="black"; 
    } 
} 

var b = false; 
function toggleblue() { 
    console.log("clicked"); 

    b=!b; 
    if(b){ 
     document.getElementById("entertext").style.color="blue"; 
    } 
    else{ 
     document.getElementById("entertext").style.color="black"; 
    } 
} 

var g = false; 
function togglegreen() { 
    g=!g; 
    if(g){ 
     document.getElementById("entertext").style.color="green"; 
    } 
    else{ 
     document.getElementById("entertext").style.color="black"; 
    } 
} 

var imp = false; 
function togglefontA(){ 
    imp = !imp; 
    if(imp){ 
     document.getElementById("entertext").style.fontFamily= "Impact,Charcoal,sans-serif"; 
    } 
} 


var tim = false; 
function togglebfontB(){ 
    tim=!tim; 
    if(tim){ 
     document.getElementById("entertext").style.fontFamily = "Times New Roman, Times, serif"; 
    } 
} 

var age = false; 
function togglefontC(){ 
    age=!age; 
    if(age){ 
     document.getElementById("entertext").style.fontFamily = "Agency FB, verdana, helvetica, sans-serif"; 
    } 
} 

var nor = false; 
function togglefontD(){ 
    nor=!nor; 
    if(nor){ 
     document.getElementById("entertext").style.fontFamily = "Arial, Helvetica, sans-serif"; 
    } 
} 


function rgb(){ 
    if(check()){ 
     document.getElementById("entertext").style.backgroundColor = 
      "rgb("+document.getElementById("red").value+"," 
      + document.getElementById("green").value+"," 
      + document.getElementById("blue").value+")"; 
    } 
    else{ 
     document.getElementById("entertext").style.backgroundColor = "white"; 
    } 
} 

function check(){ 
    var red= document.getElementById("red").value; 
    var green= document.getElementById("green").value; 
    var blue= document.getElementById("blue").value; 
    return red <= 255 && red >= 0 && green <= 255 && green >= 0 && blue <= 255 && blue >= 0; 
} 

    </script> 

因此,我的問題是,這段代碼在我的個人筆記本電腦上的Internet Explorer上工作,但不是在Chrome上。在我的uni電腦上,代碼在Chrome或Internet Explorer上完全不起作用。我試圖通過代碼尋找錯位的括號,但沒有能夠找到問題。這發生在之前,問題是一個錯位或丟失括號。如果有人可以通過這段代碼並告訴我什麼是錯的或失蹤?我有這個代碼,但我不知道爲什麼它不會工作有人可以告訴我嗎?

+1

很難通過查看代碼來判斷一個代碼*有什麼不對,我建議您在打開瀏覽器的開發者控制檯並查找錯誤時,這些通常非常有用,並且可以幫助您自己解決問題,或者如果無法幫助他人查看代碼中的問題並幫助您更輕鬆。 – Frxstrem

+2

如果您a)使用突出顯示右括號/括號(即Sublime Text)的文本編輯器,以及b)如果您正確縮進,則您會更容易發現缺少的括號。 – JakeParis

+1

你甚至沒有說過問題是什麼。你需要調試它。 – Carcigenicate

回答

0

本身並不完全是答案,但是這裏有一些建議的改進,它限制了代碼的重複位數,並且沒有任何全局變量。

/* This only operates on the element with id="entertext". 
* You could easily rewrite the function to pass in an element to 
* operate on. If no 3rd parameter is passed, it won't do anything if the 
* style is not equal to the first. 
*/ 
function toggleStyle(property,ifTrueValue,ifFalseValue){ 
    var el = document.getElementById('entertext'); 
    if(el.style[property] !== ifTrueValue) 
     el.style[property] = ifTrueValue; 
    else if (typeof ifFalseValue !== 'undefined') 
     el.style[property] = ifFalseValue; 
    } 
} 

function toggleBold(){ 
    toggleStyle('fontWeight',700,200); 
} 

function toggleitalic(){ 
    toggleStyle('fontStyle','italic','normal'); 
} 

function toggleline() { 
    toggleStyle('textDecoration','underline','none'); 
} 

function togglered() { 
    toggleStyle('color','red','black'); 
} 

function toggleblue() { 
    toggleStyle('color','blue','black'); 
} 

function togglegreen() { 
    toggleStyle('color','green','black'); 
} 

function togglefontA(){ 
    toggleStyle('fontFamily',"Impact,Charcoal,sans-serif") 
} 


function togglebfontB(){ 
    toggleStyle('fontFamily',"Times New Roman, Times, serif"); 
} 

function togglefontC(){ 
    toggleStyle('fontFamily',"Agency FB, verdana, helvetica, sans-serif"; 
} 

function togglefontD(){ 
    toggleStyle('fontFamily',"Arial, Helvetica, sans-serif"); 
} 


function rgb(){ 
    var rgb = getRGB() 
     color; 

    if(rgb == false) 
     color = 'white'; 
    else 
     color = "rgb("+rgb.red+","+rgb.green+","+rgb.blue+")"; 
    toggleStyle('backgroundColor', color); 
} 


function getRGB(){ 
    var red= document.getElementById("red").value; 
    var green= document.getElementById("green").value; 
    var blue= document.getElementById("blue").value; 
    if(red <= 255 && red >= 0 && green <= 255 && green >= 0 && blue <= 255 && blue >= 0) { 
     return { 
      'red': red, 
      'green': green, 
      'blue': blue 
     } 
    } else 
     return false; 
} 
相關問題