使用jQuery元素访问html元素?

时间:2011-08-02 17:43:41

标签: jquery knockout.js

在我看来,我有:

<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) } }">

然后在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
}

如果code == 13,意味着返回/输入,我想模糊输入框并突出显示它。鉴于传递了jQuery事件e,我该如何做$(e.html?).focusOut()之类的事情?

e看起来像这样:

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: undefined
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

1 个答案:

答案 0 :(得分:1)

如果你想在触发事件的输入上模拟一个focusOut并添加类'highlight',你可以这样做:

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

所以你的代码看起来像

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

}