Как я могу получить innerHTML текста, который перезаписан с помощью JavaScript - PullRequest
0 голосов
/ 29 декабря 2018

HTML:

<div class="col answer">
    some text
</div>

js:

$(".answer").text("Here we change the text" );
$(".answer").click(function(E) {
    console.log(E).html();
  });

консольный вывод: не определено

Ответы [ 3 ]

0 голосов
/ 29 декабря 2018

Так как это было также размещено в Javascript, вот вариант ванили, чтобы сделать то же самое:

<div class="col answer">
    some text
</div>

<script>

    var x = document.getElementsByClassName("answer");
    var y;
    for (y = 0; y < x.length; y++) {
      x[y].innerHTML = "Here we change the text";
    } 

    window.onload = function() {
        document.addEventListener('click', function (event) {
        	var target = event.target;
        	if(target.matches('.answer')){
          	console.log(target.innerHTML);
          }
        }, false);
    }
</script>
0 голосов
/ 29 декабря 2018

Вам нужно innerHTML или text ??Если вам нужен текст, используйте метод .text().

$(".answer").text("Here we change the text" );
$(".answer").off().on('click',function() {
    console.log($(this).html());
});
0 голосов
/ 29 декабря 2018

Вам нужно использовать $(this).html(), а не (E).html().Ваш код должен выглядеть следующим образом:

console.log($(this).html());

Демонстрация:

$(".answer").text("Here we change the text");
$(".answer").click(function() {
  console.log($(this).html());
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div class="col answer">
  some text
</div>
...