刚开始使用 dartlang 并稍微扩展了项目示例。似乎元素类的焦点方法不起作用,或者我没有正确使用它。想要通过“关注”“错误”来获得验证行为。这是我的代码...
#import('dart:html');
num rotatePos = 0;
var nameField;
const DEFAULT_VALUE = "Click here!";
void main() {
query("#text").text = DEFAULT_VALUE;
query("#submit").on.click.add(rotateText);
}
void rotateText(Event event) {
rotatePos += 360;
nameField = query("#name");
var textElement = query("#text");
String insertedName = nameField.value;
textElement.style.transition = "1s";
textElement.style.transform = "rotate(${rotatePos}deg)";
updateFromElement(textElement, insertedName);
}
void updateFromElement(Element textElement, String value) {
Element errorField = query("#errorValue");
if (value == "") {
errorField.text = "Text Field is empty, please insert a value!";
errorField.style.color = "red";
textElement.text = DEFAULT_VALUE;
textElement.focus();
} else {
textElement.text = value;
errorField.text = "";
}
}
Element.focus() 似乎不起作用,或者我在这里误解了一些东西。还添加了 HTML 文件以便更好地理解:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sandbox</title>
<link rel="stylesheet" href="sandbox.css">
</head>
<body>
<h1>Sandbox</h1>
<p>Dart Sandbox</p>
Enter any text: <input type="text" id="name"> <input type="button" value="Submit" id="submit"> <div id="error"><p id="errorValue"></div>
<div id="container">
<p id="text"></p>
</div>
<script type="application/dart" src="web/sandbox.dart"></script>
<script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
</body>
</html>