我正在使用聚合物的iron-autogrow-textarea。我能夠設置自動對焦屬性和它的工作非常好。Polymer autogrow textarea focus()不起作用
但是,當我嘗試將焦點設置回textarea它似乎不工作。
我已經試過
autoTextArea.focus();
它沒有工作
setTimeout(function() {
$('#autoTextArea')[0].focus();
}, 1000);
這不起作用
setTimeout(function() {
$('#autoTextArea')[0].setAttribute('autofocus', true);
}, 1000);
這顯然沒有工作的自動對焦功能僅適用於準備()。
我也嘗試訪問autogrow-textarea裏面的textArea,甚至看起來沒有工作。
有沒有辦法可以做到這一點?
在此先感謝。
這裏是我使用它的代碼片段。
'click #chatEnter': function(e, template) {
var chatArea = $('#chatArea')[0];
var chatTextArea = $('#chatTextArea')[0];
if(chatTextArea.bindValue)
{
var chatNode = document.createElement('chat-message');
chatNode.setAttribute('color', '#ff00ff');
chatNode.setAttribute('avatar', '/src/someimage.jpg');
chatNode.setAttribute('username', 'SomeName1');
chatNode.setAttribute('text', chatTextArea.bindValue);
chatNode.setAttribute('status',"MyStatus");
chatNode.setAttribute('timestamp',"2015-07-12 12:00:00 AM");
chatArea.appendChild(chatNode);
chatTextArea.bindValue = "";
setTimeout(function() {
$('#chatTextArea')[0].setAttribute('autofocus', true);//.focus();
}, 1000);
}
這裏是我使用它的HTML。
<section main layout vertical id="chat">
<paper-material id="chatArea" elevation="1" animated style="overflow-y:scroll">
</paper-material>
<span layout horizontal>
<paper-toolbar class="medium">
<div>
<iron-autogrow-textarea label="Enter message here" autocomplete="true" autofocus="true" maxRows=5 name="Text Area" id="chatTextArea">
<textarea id="chatText" max-rows="5" ></textarea>
</iron-autogrow-textarea>
</div>
<paper-icon-button raised icon="send" id="chatEnter"></paper-icon-button>
<iron-a11y-keys keys="ctrl+enter" on-keys-pressed="[[enterKeyHandler]]"></iron-a11y-keys>
</paper-toolbar>
</span>
</section>
你能提供更多的代碼嗎?你在使用jQuery嗎? –
我正在使用Polymer和Jquery(不是很重)。這裏是代碼。 – IvoryHeart