无法阅读财产'焦点'为null

时间:2016-10-13 14:09:18

标签: javascript jquery browser

我正在尝试关注输入窗口,但它给出错误:enter image description here

<div class="intercom-conversation-footer" data-
    reactid=".2.1.$=1$conversation.2"><div class="intercom-composer" data-
    reactid=".2.1.$=1$conversation.2.0"><pre data-
    reactid=".2.1.$=1$conversation.2.0.0"><span data-
    reactid=".2.1.$=1$conversation.2.0.0.0">write something</span><br data-
    reactid=".2.1.$=1$conversation.2.0.0.1"></pre><textarea placeholder="Write 
    a reply..." data-reactid=".2.1.$=1$conversation.2.0.1"> </textarea><span
     data-reactid=".2.1.$=1$conversation.2.0.2"></span><button class="intercom-
    composer-emoji-button" data-reactid=".2.1.$=1$conversation.2.0.5"></button>
    <button class="intercom-composer-send-button" data-
    reactid=".2.1.$=1$conversation.2.0.7"></button></div></div>

我想专注于这个我怎么做?

我的代码是:

var message = document.createEvent("TextEvent");  
message.initTextEvent ("textInput", true, true, window, "Hello", 0, "en-US");
document.getElementById(".2.1.$=1$conversation.2.0.0.0").focus();

但是给出错误:

VM20681:1 Uncaught TypeError: Cannot read property 'focus' of null(…)

2 个答案:

答案 0 :(得分:1)

您没有ID,但只有数据重新属性。所以,你可以使用像: document.querySelector(&#34;跨度[数据reactid =&#39; .2.1 $ = 1 $ conversation.2.0.0.0&#39;&#34)聚焦();

答案 1 :(得分:0)

您可以使用jQuery&#39; attribute selector

$('[data-reactid=".2.1.$=1$conversation.2.0"]').focus();