用于动态添加样式到锚标记的JQuery代码

时间:2015-03-31 06:27:09

标签: jquery html css anchor

我正在通过我的代码动态构建锚标记。

 str += " <li><a href='" + hyperlink + "'>" + linkName + "</a></li>";

我想在特定条件下将以下样式应用于此锚标记。

style="pointer-events: none;cursor: default;"

If (somecond) { apply the above style to anchor tag } 

请建议如何实现这一目标。

提前致谢。

3 个答案:

答案 0 :(得分:1)

使用jquery .css()函数(see docs),例如:

if(somecond) {
    $('a').css({
      "pointer-events": "none",
      "cursor": "default"
    });;
}

答案 1 :(得分:0)

$("a").css("pointer-events","none");
$("a").css("cursor","default");

希望这可能会让你满意

答案 2 :(得分:0)

你可以这样做。

If(Your Condition Matching Criteria)
{
   $("li a").css("pointer-events","none");
   $("li a").css("cursor","default");
}

但是上面的解决方案会对所有页面级别的锚点执行此操作,这些锚点位于&#39; li&#39;标记所以你可以为li的特定父元素做什么,你可以提供唯一标识符然后可以这样做。

If(Your Condition Matching Criteria)
    {
       $("#uniqueParentId li a").css("pointer-events","none");
       $("#uniqueParentId li a").css("cursor","default");
    }

替换&#34; uniqueParentId&#34;与您提供唯一的父ID值。

相关问题