开发者

Accessing an html element with a jQuery Element?

In my view I have:

<input class="list_item_title" 
       name="list_item[title]" 
       type="text" 
       data-bind="value: title, 
                  valueUpdate: 'afterkeydown', 
                  event: { keypress: function(event) { return appListModel.magicMan(event, $data) } }">

Then in JS:

magicMan : function(e, list_item) {
    CL.log(e)

    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        CL.log(list_item.title());
    }

    return true
}

If code == 13, meaning a return/enter, I want to blur out of the input box and highlight it. given the jQuery event being passed, e, how can I do something like $(e.html?).focusOut()?

e looks like this:

jQuery.Event
altKey: false
attrChange: undefined
attrName: undefined
bubbles: true
button: undefined
cancelable: true
charCode: 97
clientX: undefined
clientY: undefined
ctrlKey: false
currentTarget: HTMLInputElement
data: undefined
detail: 0
eventPhase: 2
fromElement: undefined
handleObj: Object
data: undefined
guid: 56
handler: function (event) {
namespace: ""
type: "keypress"
__proto__: Object
handler: function (event) {
isDefaultPrevented: function returnFalse() {
jQuery16108603769612964243: true
keyCode: 97
layerX: 0
layerY: 0
metaKey: false
newValue: undefined
offsetX: undefi开发者_如何学Pythonned
offsetY: undefined
originalEvent: KeyboardEvent
pageX: 0
pageY: 0
prevValue: undefined
relatedNode: undefined
relatedTarget: undefined
screenX: undefined
screenY: undefined
shiftKey: false
srcElement: HTMLInputElement
target: HTMLInputElement
timeStamp: 1312306975744
toElement: undefined
type: "keypress"
view: DOMWindow
wheelDelta: undefined
which: 97
__proto__: Object


You could do, if you want to simulate a focusOut on the input that triggered the event and add the class 'highlight':

 $(e.target).trigger('blur').addClass('higlight');

So your code would look

magicMan : function(e, list_item) {

    CL.log(e)

    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        CL.log(list_item.title());
        $(e.target).trigger('blur').addClass('higlight');
    }

    return true

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜