如何从列表类型中删除特定属性名称

时间:2017-06-23 07:18:52

标签: jquery html

如何删除列表类型具有特定的属性名称,我试图从动态变量中删除具有属性名称的特定li。如何解决这个问题

var Attribute = 2;
<ul>
<li name="1">as</li>
<li name="2">asd</li>
<li name="3">asdf</li>
</select>
$("ul li").each(function () {
if($(this).hasAttr("name",Attribute){
$(this).remove();
}
else{
alert("no attribute present");
}
});

3 个答案:

答案 0 :(得分:1)

这有两个部分:

  1. 查找li

    var li = $('li[name="' + Attribute + '"]');
    

    注意字符串连接。它生成一个像li[name="2"]这样的字符串。在您的示例中,我们不需要围绕属性值",但如果它包含空格或其他字符,我们最好包含它们。

    这是一个CSS选择器。关于他们的更多信息here

  2. 使用remove删除它:

    li.remove();
    
  3. 当然可以合并:

    $('li[name="' + Attribute + '"]').remove();
    

    直播示例:

    &#13;
    &#13;
    setTimeout(function() {
        var Attribute = 2;
        $('li[name="' + Attribute + '"]').remove();
    }, 800);
    &#13;
    <ul>
    <li name="1">as</li>
    <li name="2">asd</li>
    <li name="3">asdf</li>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

您可以使用with alldates as ( SELECT convert(date,thedate) alldate FROM dbo.ExplodeDates( '2017-06-10' , '2017-06-10') as d ), empshift as ( select ed.Pk,ed.EmployeeName,sd.ShiftName,sd.Intime,sd.OutTime from EmployeeDetails ed left outer join EmployeeShiftDetails esd on (ed.Pk = esd.EmployeePk) left outer join ShiftDetails sd on (sd.Pk = esd.ShiftPk ) ) ,biometric1 as ( select min(punchtime) intime,max(punchtime) outtime,convert(date,punchtime) dates , empshift.EmployeeName,empshift.ShiftName,Intime ShiftInTime,OutTime ShiftOutTime from BioMetricPunchDetails bpd inner join empshift on (empshift.Pk =bpd.EmployeePk ) where CONVERT(date,punchtime) between '2017-06-10' and '2017-06-0' group by convert(date,punchtime), empshift.EmployeeName,empshift.ShiftName,Intime,OutTime ), fnl as ( select convert(date,intime) dates,intime, case when (intime = outtime ) then NULL else outtime end as outtime ,ShiftName,DATEDIFF(MINUTE,intime,outtime) duration,ShiftInTime,convert(time,intime) InTimeHrMin ,datediff(MINUTE,ShiftInTime, convert(time,intime)) lateby from biometric1 ), fnl1 as (select * from alldates left outer join fnl on (alldates.alldate = fnl.dates)) select ed.EmployeeName,fnl1.* from EmployeeDetails ed cross join fnl1 检查属性。 attr()为您提供的属性值。

&#13;
&#13;
attr()
&#13;
var Attribute = 2;

$("ul li").each(function () {

  if($(this).attr('name') == Attribute){
     $(this).remove();
   }
   else{
     //alert("no attribute present");
   }
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

尝试使用简单的直接选择器$("ul").find('li[name="'+Attribute+'"]').remove()

&#13;
&#13;
var Attribute = 2;
$("ul").find('li[name="'+Attribute+'"]').remove()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li name="1">as</li>
  <li name="2">asd</li>
  <li name="3">asdf</li>
</ul>
&#13;
&#13;
&#13;