我的代码无法识别jQuery选择器?

时间:2019-08-14 10:44:23

标签: jquery html ajax typescript powerbi

我想在代码自动生成的特定<div>中获取值,但是当我使用jQuery语法时,该语法似乎无法识别带有id标记的div。

HTML代码保存在Variable中,下面是一个<div>的td标签封面,我想在此<div>中获取数据!字符串插值(${count})是正确的-我已经通过将其插入<div></div>进行了测试,并且其输出是正确的,该输出在上面的for循环的基础上从1增大到n以初始化{{1 }}。

<div>
<td><div id='${count}' contenteditable='true'>editable</div></td>

有人可以帮我吗?!

2 个答案:

答案 0 :(得分:1)

您可以使用this关键字来引用引发事件的内容可编辑的div元素。从那里可以使用html()来获取其内容。

还要注意,您的逻辑暗示div是动态附加到DOM的。这样,您将需要使用委托的事件处理程序,因为页面加载时该元素不存在。试试这个:

$(document).on('keypress', '#1', function(e) {
  if (e.which === 13) {
    var content = $(this).html();
    console.log(content);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <div id="1" contenteditable="true">editable</div>
    </td>
  </tr>
</table>

答案 1 :(得分:0)

希望这是您想要的。 RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTPS}s on(s)|offs() RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R] RewriteRule \.(gif|jpg|png|css|flv|js|swf|php|ico|webm|mp4)$ - [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+\.php)$ index.php?url=$1 [QSA,L] RewriteCond %{REQUEST_URI} !(\.) RewriteCond %{REQUEST_URI} !(\.) RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} .* RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 事件适用于kepress

input
var count = 1;

var HTML = '<input id=' + count + ' contenteditable="true" placeholder="Press Enter">';

$(".test").html(HTML)

$("#1").keypress(function(e) {
  if (e.which === 13) {
    alert(1)
  }
});