Javascript 从类中获取值(value)

标签 javascript jquery ajax

我正在尝试将其用于类作业,我仍在学习中。

此脚本用于检查站点是否关闭。

我想要实现的是使用相同的脚本并获取表中每个网站的状态,而无需在所有 3 个网站上再次使用脚本。

我添加了类,但我不知道如何让它们编写脚本。我对脚本进行了评论,以便您更容易理解。有什么解决办法吗?任何输入将不胜感激。

<table>
    <thead>
        <tr>
            <th style="width:250px">Website</th>
            <th style="width:250px">Is it live</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Google</td>                                   

            <td id="website"></td>  <!-- This is already working. -->

        </tr>
        <tr>
            <td>Twitter</td>

            <td id="website" mywebsite="https://Twitter.com"></td> <!-- This is what I'm trying to achive. -->


        </tr>
        <tr>
            <td>Facebook</td>

            
            <td id="website" mywebsite="https://Facebook.com"> <!-- This is what I'm trying to achive. -->

        </tr>
    </tbody>
</table>





<script src="https://code.jquery.com/jquery-1.12.4.js"></script>



<script type="text/javascript">
    var site = 'https://google.com';



    // var site = 'https://google.com';
    
    $.ajax
    ({
      url: site,
      dataType: "jsonp",
      statusCode: {
          200: function (response) {
              $('#website').html('yes');
            console.log(response);
          },
          404: function (response) {
              $('#website').html('no');
            console.log(response);
          }
      } 
     });
</script>

最佳答案

<table>
        <thead>
            <tr>
                <th style="width:250px">Website</th>
                <th style="width:250px">Is it live</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Google</td>                                   

                <td class="website" data-mywebsite="https://google.com"></td>  <!-- This is already working. -->

            </tr>
            <tr>
                <td>Twitter</td>

                <td class="website" data-mywebsite="https://twitter.com"></td> <!-- This is what I'm trying to achive. -->


            </tr>
            <tr>
                <td>Facebook</td>


                <td class="website" data-mywebsite="https://facebook.com"> </td>

            </tr>
        </tbody>
    </table>



$(document).ready(function(){
         $(".website").each(function(){
           var site = $(this).attr("data-mywebsite");
           var thissr = $(this);

        $.ajax
        ({
          url: site,
          dataType: "jsonp",
          statusCode: {
              200: function (response) {
                  thissr.html('yes');

              },
              404: function (response) {
                  thissr.html('no');

              }
          } 
         });
         });
       });

试试这段代码。这是一个 fiddle

您需要使用类并循环遍历它们中的每一个以发送 AJAX 请求。

关于Javascript 从类中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49796343/

相关文章:

javascript - 如何将参数传递给 MEAN.js 包的express.js 服务器端路由。

javascript - 包含其他 javascript 文件时 Highcharts javascript 错误

javascript - Mouseenter 内部的函数不改变 Attr

javascript - 当我通过 jQuery/javascript 传递条件单选按钮时,为什么我的 CSS 样式表没有反射(reflect)出来?

javascript - Angularjs - 取消选中复选框时如何返回到之前的状态

javascript - jquery ajax获取返回值

javascript - 在 Canvas 上绘制一个充满辐射的 Angular/弧线?

javascript - Canvas 还是PNG? (适用于 html5 动画)

javascript - localStorage 和大于和小于 slider

javascript - ajax后的 Angular 数据绑定(bind)