【问题标题】:How to get all the texts from html page having same id with JavaScript?如何从具有与 JavaScript 相同 id 的 html 页面中获取所有文本?
【发布时间】:2021-01-06 00:46:36
【问题描述】:

我有这个代码的网页:-

<article class="media content-section">
    <div class="media-body">
        <h2><a id="post_title" class="article-title" href="/post/new/">new</a></h2>
        <div class="article-metadata">
            <a class="mr-2" href="/profileview/manish">manish</a>
            <div class="float-right">
                <small class="text-muted">Category</small>
                <small class="text-muted">Sept. 19, 2020, 2:49 a.m.</small>
            </div>
            <div style="float:right;">
                <img style="height:19px; width:18px;" src="/static/blog/viewicon.png">
                    <p style="float: right; display: inline !important;" id="ViewCount">
                    
                    </p>
                </img>
            </div>
        </div>
        <p class="article-content"><p>newnwnewnenwnenwenwewewewewe</p></p>
    </div>
</article>

<article class="media content-section">
    <div class="media-body">
        <h2><a id="post_title" class="article-title" href="/post/post-with-markdown-styling/">Post with Markdown Styling?</a></h2>
        <div class="article-metadata">
            <a class="mr-2" href="/profileview/manish">manish</a>
            <div class="float-right">
                <small class="text-muted">Category</small>
                <small class="text-muted">Sept. 15, 2020, 6:46 p.m.</small>
            </div>
            <div style="float:right;">
                <img style="height:19px; width:18px;" src="/static/blog/viewicon.png">
                    <p style="float: right; display: inline !important;" id="ViewCount">
                    
                    </p>
                </img>
            </div>
        </div>
        <p class="article-content"><p>This are the points which you should follow while writting a good blog:- Have a proper Introduction.&nbsp;Be explainatory.&nbsp;Use easy english language.&nbsp;Be consise and clear.&nbsp;Have a go…</p>
    </div>
</article>
------
------
------

我正在尝试通过向我的 JavaScript 代码添加一个条件来在包含 id ViewCount 的标签上添加一个文本,每当从服务器获取的 title 计算页面标题时。
这是我的 JavaScript 代码:-

    $(document).ready(function(){

        setInterval(function()
        {
      $.ajax({
          type:'GET',
          url: "{% url 'getBlogs' %}",
          success: function(response){
          $("#ViewCount").empty();
          for (var key in response.blogs)
          {
              var fromServerTitle = response.blogs[key].title;
              var fromPageTitle = document.getElementById("post_title").innerText;
              if (fromServerTitle == fromPageTitle) {
                  var temp = response.blogs[key].view_count
                  $("#ViewCount").append(temp);
              }
              //console.log(key);
           }
          },
          error:function(response){
              alert("No Data Found");
          }
      });
        },1000);
    });

但使用此代码,它仅检查第一个 title,并进行所需的更改,但我希望这适用于所有此类 id's。

这是截图:-

模板代码(html):-

{% for post in posts %}
<article class="media content-section">
    <div class="media-body">
        <h2><a id="post_{{post.pk}}" class="article-title" href="{% url 'post-detail' post.slug %}">{{ post.title }}</a></h2>
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'blog-profile' name=post.author %}">{{ post.author }}</a>
            <div class="float-right">
                <small class="text-muted">Category</small>
                <small class="text-muted">{{ post.date_posted }}</small>
            </div>
            <div style="float:right;">
                <img style="height:19px; width:18px;" src="{% static "blog/viewicon.png" %}">
                    <p style="float: right; display: inline !important;" id="ViewCount">
                    
                    </p>
                </img>
            </div>
        </div>
        <p class="article-content">{{ post.content|truncatechars:200|safe }}</p>
        <hr />
        <button class="btn btn-light btn-sm disabled" type="submit">❤</button>
        <button class="btn btn-light btn-sm disabled" type="submit">????</button>
        <button class="btn btn-light btn-sm disabled" type="submit">????</button>
    </div>
</article>
{% endfor %}
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script>
    $(document).ready(function(){

        setInterval(function()
        {
      $.ajax({
          type:'GET',
          url: "{% url 'getBlogs' %}",
          success: function(response){
          $("#ViewCount").empty();
          for (var key in response.blogs)
          {
              var fromServerTitle = response.blogs[key].title;
              var initialstr = "post_"
              var finalstr = initialstr.concat(response.blogs[key].pk)
              var fromPageTitle = document.getElementById(finalstr).innerText;
              if (fromServerTitle == fromPageTitle) {
                  var temp = response.blogs[key].view_count
                  $("#ViewCount").append(temp);
              }
              //console.log(key);
           }
          },
          error:function(response){
              alert("No Data Found");
          }
      });
        },1000);
    });
</script>

控制台的输出:-

(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116
(index):186 Uncaught TypeError: Cannot read property 'innerText' of null
    at Object.success ((index):186)
    at fire (jquery-3.5.1.js:3496)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.js:3626)
    at done (jquery-3.5.1.js:9786)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.js:10047)
success @ (index):186
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
done @ jquery-3.5.1.js:9786
(anonymous) @ jquery-3.5.1.js:10047
load (async)
send @ jquery-3.5.1.js:10066
ajax @ jquery-3.5.1.js:9682
(anonymous) @ (index):176
setInterval (async)
(anonymous) @ (index):174
mightThrow @ jquery-3.5.1.js:3762
process @ jquery-3.5.1.js:3830
setTimeout (async)
(anonymous) @ jquery-3.5.1.js:3868
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
fire @ jquery-3.5.1.js:3634
fire @ jquery-3.5.1.js:3496
fireWith @ jquery-3.5.1.js:3626
ready @ jquery-3.5.1.js:4106
completed @ jquery-3.5.1.js:4116

【问题讨论】:

  • 你为什么要单独获取这个?您已经从服务器呈现信息,因此只需将其添加到模板中。当您使用它时,请修复您的 ID。它们在每个页面上必须是唯一的。
  • 'viewcount' 不会异步更新,我必须手动刷新页面,所以我使用 ajax 来同步该值。
  • 明白了。就像我说的,修复你的 ID:ID 是标识符,每页唯一。您现在有无效的 HTML。类是“共享公共属性的事物”,“帖子标题”是一个公共属性,但每个标题都需要有其唯一的 ID,例如帖子的主键:id="post-{{ post.pk }}"。如果您希望更好地解释,请共享模板而不是呈现的 HTML。
  • 嘿,我正在遵循您的方法,但实际上无法弄清楚如何实现它,我是 Javascript 的初学者,如果您能帮我解决这个问题,那将非常有帮助。我已经用模板更新了问题。
  • 还有一件事:你能发布 ajax 调用的输出吗?

标签: javascript html django ajax web


【解决方案1】:

尝试使用类而不是 id。它在页面中应该是唯一的。如果您想要多个具有相同标识的元素,请使用具有相同名称的类

而不是

<tag id="post_title"></tag>

使用

<tag class="post_title"></tag>

并在 javascript 中更改此行

 var fromPageTitle = document.getElementsByClassName("post_title").innerText;

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2022-01-18
    • 1970-01-01
    • 2015-09-02
    相关资源
    最近更新 更多