sh3*_*ans 2 javascript jquery angularjs
我正在尝试创建一个自定义指令,其中包含一个文本框,当用户单击按钮时,该文本框会显示,展开和聚焦.当用户点击扩展文本框时,它应该最小化,消失,然后显示原始按钮.这是我到目前为止所拥有的:http: //jsfiddle.net/Z6RzD/152/
以下是我遇到问题的部分:
if (htmlTxtBox.addEventListener !== undefined) {
console.log("first");
htmlTxtBox.addEventListener('focus', setFocus(), false);
//this function is automatically called even when textBox is in focus
//htmlTxtBox.addEventListener('blur', setNoFocus(), false);
console.log("ifHasFocus: " + scope.showInput);
} else if (htmlTxtBox.attachEvent != undefined) {
console.log("second");
htmlTxtBox.attachEvent('onfocus', setFocus());
htmlTxtBox.attachEvent('onblur', setNoFocus());
} else {
console.log("third");
htmlTxtBox.onmouseover = setFocus();
htmlTxtBox.onmouseout = setNoFocus();
}
Run Code Online (Sandbox Code Playgroud)
以及与事件侦听器一起运行的相应函数:
function setFocus() {
scope.$apply('showInput = true');
console.log("setFocus: " + scope.showInput);
}
function setNoFocus(){
scope.$apply('showInput = false');
console.log("setNoFocus: " + scope.showInput);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是setFocus和setNoFocus函数无论如何运行.如何在文本框聚焦或模糊时将这些函数构建到它们运行的位置?我感谢任何帮助.谢谢!
试试这个:
myApp.directive('replybox', function($timeout) {
var linkFn = function(scope, element, attrs) {
scope.showInput = false;
var button = element.find('button');
var input = element.find('input');
button.bind("click", function() {
scope.showInput = true;
scope.$apply();
input[0].focus();
input.css({"width":"300px","transition":"1s"});
});
input.bind('blur', function() {
scope.showInput = false;
$timeout(function() { scope.$apply(); }, 1000);
input.css({'width':'0px', 'transition':'1s'});
});
};
...
Run Code Online (Sandbox Code Playgroud)
jsFiddle 在这里.
| 归档时间: |
|
| 查看次数: |
20143 次 |
| 最近记录: |