我不明白我做了什麼錯誤,當我把內聯的JavaScript和創建一個外部js文件。不能讓它工作。它工作時,它是所有的HTML,但當我把它移動我的按鈕不再工作。誰能幫忙?從內嵌JavaScript到外部文件,爲什麼不工作?
$().ready(init);
function init()
{
$j('.num button:not(:contains(+/-),:contains(.))').click(addDigit);
$j('.num button:contains(.)').click(addDecimal);
$j('.num button:contains(+/-)').click(switchSign);
$j('.operator button:not(:contains(=),:contains(C))').click(applyOperator);
$j('.operator button:contains(=)').click(displayResult);
$j('.operator button:contains(C)').click(clearInput);
}
function addDigit()
{
Screen.append($j(this).text());
}
function addDecimal()
{
var Input = Screen.readValue().split(' ');
var FinalExpr = Input[ Input.length-1 ];
Input[ Input.length-1 ] = FinalExpr.replace('\.','') + '.';
Screen.set(Input.join(' '));
}
function switchSign()
{
var Input = Screen.readValue().split(' ');
var FinalExpr = Input[ Input.length-1 ];
if (FinalExpr.charAt(0) == '-')
{
FinalExpr = FinalExpr.substring(1 , FinalExpr.length);
}
else
{
FinalExpr = '-' + FinalExpr;
}
Input[ Input.length-1 ] = FinalExpr;
Screen.set(Input.join(' '));
}
function applyOperator()
{
Screen.append(' '+$j(this).text()+' ');
}
function displayResult()
{
Screen.displayResult();
}
function clearInput()
{
Screen.clear();
}
var Screen =
{
init:
function()
{
return this;
}
, ScreenSelector : '.results input:text'
, ResetNextAppend : true
, set:
function(value)
{
$j(this.ScreenSelector).val(value);
}
, append:
function(value)
{
if (this.ResetNextAppend == true || $j(this.ScreenSelector).val() == 0)
{
if (value.substring(0,1).search(/\d/) != -1)
{
this.clear();
}
this.ResetNextAppend = false;
$j(this.ScreenSelector).removeClass('rna');
};
$j(this.ScreenSelector).val($j(this.ScreenSelector).val() + value);
}
, readValue:
function()
{
return $j(this.ScreenSelector).val();
}
, readResult:
function()
{
return this.calculate(this.readValue());
}
, displayResult:
function()
{
this.ResetNextAppend = true;
$j(this.ScreenSelector).addClass('rna');
$j(this.ScreenSelector).val(this.readResult());
}
, calculate:
function(expression)
{
return eval(this.convertChars(expression));
}
, convertChars:
function(text)
{
text = text.replace(String.fromCharCode(215),'*');
text = text.replace(String.fromCharCode(247),'/');
return text;
}
, clear:
function()
{
this.ResetNextAppend = false;
$j(this.ScreenSelector).removeClass('rna');
$j(this.ScreenSelector).val('');
}
};
和我的HTML代碼是:
<link rel="stylesheet" type="text/css" href="A05ReDoCSS.css">
</head>
<body>
<h1>Sara's Calculator</h1>
<div id="A05">
<fieldset class="results"> <!-- use of fieldset keeps out a bunch of divs -->
<input type="text"/>
</fieldset>
<fieldset class="num pad">
<button type="button">1</button>
<button type="button">2</button>
<button type="button">3</button>
<button type="button">4</button>
<button type="button">5</button>
<button type="button">6</button>
<button type="button">7</button>
<button type="button">8</button>
<button type="button">9</button>
<button type="button">0</button>
<button type="button">+/-</button> <!-- switch -->
<button type="button">.</button>
</fieldset>
<fieldset class="operator pad"> <!-- this is the operations area -->
<button type="button">+</button>
<button type="button">-</button>
<button type="button">×</button>
<button type="button">÷</button>
<button type="button">=</button>
<button type="button">C</button>
</fieldset>
</div>
<br class="break"/>
<script type="text/javascript" src="A05ReDoJS.js"></script>
</body>
</html>
什麼不工作?你是否得到了具體的錯誤,或者什麼都沒有發生? –
你想要調用一個函數嗎? –
你在腳本中包含jQuery嗎?什麼是$ j或$()。ready(init);任何控制檯錯誤? –