Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript添加属性_Javascript_Jquery - Fatal编程技术网

如何使用javascript添加属性

如何使用javascript添加属性,javascript,jquery,Javascript,Jquery,如何使用javascript添加属性: 代码如下: <ul class="nav nav-tabs"> <li class="active"> <a href="#tab_1_4" data-toggle="tab" id="8" onclick="TableIdByNew(this.id)"><span class="badge badge-default" id="sipp-count">0</span> New <

如何使用javascript添加属性:

代码如下:

<ul class="nav nav-tabs">
  <li class="active">
    <a href="#tab_1_4" data-toggle="tab" id="8" onclick="TableIdByNew(this.id)"><span class="badge badge-default" id="sipp-count">0</span> New </a>

  </li>
  <li>
    <a href="#tab_1_2" data-toggle="tab" id="9" onclick="TableIdByInProgress(this.id)"><span class="badge badge-default" id="sass-count">0</span> InProgress </a>
  </li>
  <li>
    <a href="#tab_1_3" data-toggle="tab" id="10" onclick="TableIdByClosed(this.id)"><span class="badge badge-default" id="sass-counts">0</span> Closed </a>
  </li>
</ul>
但这是行不通的

这是表格:

<div class="tab-pane active in" id="tab_1_4" style="display:none">
                        <p>
                            <div class="portlet box">
                                <div class="portlet-title">


                                    <table class="table table-striped table-bordered dt-responsive table-hover" width="100%" id="AdminTicketId4" cellspacing="0">
                                        <thead>


                                            <tr>
                                                <th> Date Created </th>
                                                <th> Title </th>
                                                <th> User Name </th>
                                                <th> User Email </th>
                                                <th> Assigned To </th>
                                                <th>Importancy</th>
                                                <th> Status </th>
                                            </tr>
                                        </thead>

                                    </table>

                                </div>
                            </div>
                        </p>
                    </div>


创建日期
标题
用户名
用户电子邮件
分配给
重要性
地位

表格如下:

这是第一张桌子,好的

我如何解决

哪里错了?

$(“#8”).attr(“aria扩展”、“true”);
$(“#9”).attr(“aria扩展”、“假”);
$(“#10”).attr(“aria扩展”、“假”)

$(“#8”).attr(“aria扩展”、“true”);
$(“#9”).attr(“aria扩展”、“假”);
$(“#10”).attr(“aria扩展”、“假”)


您已经指定了id=“8”等等,因此您必须使用“#”才能与JQuery一起使用,请尝试以下操作:

$("#8").attr("aria-expanded", "true");

您已经指定了id=“8”等等,因此必须使用“#”才能与JQuery一起使用,请尝试以下操作:

$("#8").attr("aria-expanded", "true");

jQuery选择器的工作方式与CSS选择器类似


要按元素的ID选择元素(
this\u is\u-ID
),请使用
$(“'this\u is\u-ID')
。注意
-如果没有它,您将匹配类型为
的元素this\u是一个id
。把
$('li')
作为一个有意义的例子。

jQuery选择器的工作原理与CSS选择器类似


要按元素的ID选择元素(
this\u is\u-ID
),请使用
$(“'this\u is\u-ID')
。注意
-如果没有它,您将匹配类型为
的元素this\u是一个id
。把
$('li')
作为一个有意义的例子。

您问的是如何在Javascript而不是jQuery中添加它们。这是在Javascript()中完成的:


  • 。Javascript已经走过了漫长的道路。

    您问的是如何在Javascript中添加它们,而不是在jQuery中。这是在Javascript()中完成的:

    • 。Javascript已经走过了漫长的道路。

      $([a[id=8]).attr(“aria expanded”,“true”);
      $(“a[id=9]”)attr(“aria expanded”、“false”);
      $(“a[id=10]”)attr(“aria expanded”、“false”)
      
      
      
      $([a[id=8]).attr(“aria expanded”,“true”);
      $(“a[id=9]”)attr(“aria expanded”、“false”);
      $(“a[id=10]”)attr(“aria expanded”、“false”)
      
      
      

      $(“#8”)
      我建议不要把数字放在第一位。缺少
      #
      @guradio是正确的。要选择带有jquery by id的html元素,您需要hashtag
      #
      不起作用…未捕获错误:语法错误,无法识别的表达式:#[object HTMLTableElement]不起作用…未提供任何有关计算机发生的错误的详细信息。什么不起作用?你有什么错误吗?
      $(“#8”)
      我建议不要把数字放在第一位。缺少
      #
      @guradio是正确的。要选择带有jquery by id的html元素,您需要hashtag
      #
      不起作用…未捕获错误:语法错误,无法识别的表达式:#[object HTMLTableElement]不起作用…未提供任何有关计算机发生的错误的详细信息。什么不起作用?您是否收到任何错误?未捕获错误:语法错误,无法识别的表达式:#[object HTMLTableElement]@manoj-问题中显示的代码无法再现该错误。请注意,这个答案中的代码可以通过“RunCodeSnippet”按钮直接运行,它可以按原样工作。。。(在答案中稍加解释会很好,但代码仍然有效。)在你的问题中,你只提到了如何添加attr…在我的回答中,添加了此attr。未捕获错误:语法错误,无法识别的表达式:#[object HTMLTableElement]@manoj-问题中显示的代码无法再现该错误。请注意,这个答案中的代码可以通过“RunCodeSnippet”按钮直接运行,它可以按原样工作。。。(在答案中稍加解释会很好,但代码仍然有效。)在你的问题中,你只提到了如何添加属性……在我的回答中,添加了此属性。为什么不使用
      “#8”
      ?未捕获错误:语法错误,无法识别的表达式:#[object HTMLTableElement]at Function.fa.Error(jquery.min.js:2)在fa.tokenize(jquery.min.js:2)在fa.select(jquery.min.js:2)在Function.fa[as find](jquery.min.js:2)在n.fn.init.find(jquery.min.js:2)在n.fn.init(jquery.min.js:2)在n(jquery.min.js:2)在TableIdBy(adminicketlist.js:956)在htmlanchoreElement.onclick(adminicket:226)@manoj错误说明得很好,那是一张桌子
      <ul>
        <li><a id="8">New</a></li>
        <li><a id="9">InProgress</a></li>
        <li><a id="10">Closed</a></li>
      </ul>
      
      <script>
      document.getElementById('8').setAttribute('aria-expanded', 'true');
      document.getElementById('9').setAttribute('aria-expanded', 'false');
      document.getElementById('10').setAttribute('aria-expanded', 'false');
      </script>