根据javascript更改href

时间:2017-04-06 14:08:20

标签: javascript href setattribute createelement

我试图找出确定href的最佳方法,具体取决于是否启用了javascript。如果启用了javascript,我会喜欢`href ="#"'

<ul>
    <li onclick="openLandscape()"><a class="small-caps bold icon-tree" href="#"><span class="sticky">Landscaping</span></a></li>
    <li onclick="openEdible()"><a class="small-caps bold icon-apple" href="#"><span class="sticky">Edible Accents</span></a></li>
    <li onclick="openPests()">8<a class="small-caps bold icon-android" href="#"><span class="sticky">Pests</span></a></li>
    <li onclick="openProducts()"><a class="small-caps bold icon-shopping-cart" href="#"><span class="sticky">Products</span></a></li>
</ul>

如果未启用javascript,我希望href如下所示:

<ul>
    <li onclick="openLandscape()"><a class="small-caps bold icon-tree" href="#landscaping"><span class="sticky">Landscaping</span></a></li>
    <li onclick="openEdible()"><a class="small-caps bold icon-apple" href="#edible"><span class="sticky">Edible Accents</span></a></li>
    <li onclick="openPests()">8<a class="small-caps bold icon-android" href="#pests"><span class="sticky">Pests</span></a></li>
    <li onclick="openProducts()"><a class="small-caps bold icon-shopping-cart" href="#products"><span class="sticky">Products</span></a></li>
</ul>

想法会很棒。感谢。

2 个答案:

答案 0 :(得分:0)

非常简单,你可以这样做:

<script>
  (function(d){
    d.write("<ul>");
    d.write("<li onclick=\"openLandscape()\"><a class=\"small-caps bold icon-tree\" href=\"#\"><span class=\"sticky\">Landscaping</span></a></li>");
    d.write("<li onclick=\"openEdible()\"><a class=\"small-caps bold icon-apple\" href=\"#\"><span class=\"sticky\">Edible Accents</span></a></li>");
    d.write("<li onclick=\"openPests()\"><a class=\"small-caps bold icon-android\" href=\"#\"><span class=\"sticky\">Pests</span></a></li>");
    d.write("<li onclick=\"openProducts()\"><a class=\"small-caps bold icon-shopping-cart\" href=\"#\"><span class=\"sticky\">Products</span></a></li>");
    d.write("</ul>");
  })(window.document);
</script>

<noscript>
<ul>
    <li onclick="openLandscape()"><a class="small-caps bold icon-tree" href="#landscaping"><span class="sticky">Landscaping</span></a></li>
    <li onclick="openEdible()"><a class="small-caps bold icon-apple" href="#edible"><span class="sticky">Edible Accents</span></a></li>
    <li onclick="openPests()"><a class="small-caps bold icon-android" href="#pests"><span class="sticky">Pests</span></a></li>
    <li onclick="openProducts()"><a class="small-caps bold icon-shopping-cart" href="#products"><span class="sticky">Products</span></a></li>
</ul>
</noscript>

答案 1 :(得分:0)

你可以做的是使用javascript动态更新href。

这是代码

<ul>
    <li onclick="openLandscape()"><a class="small-caps bold icon-tree" href="#landscaping"><span class="sticky">Landscaping</span></a></li>
    <li onclick="openEdible()"><a class="small-caps bold icon-apple" href="#edible"><span class="sticky">Edible Accents</span></a></li>
    <li onclick="openPests()">8<a class="small-caps bold icon-android" href="#pests"><span class="sticky">Pests</span></a></li>
    <li onclick="openProducts()"><a class="small-caps bold icon-shopping-cart" href="#products"><span class="sticky">Products</span></a></li>
</ul>

//this script will run only if javascript is enabled
<script>
    //iterate over all the li elements
    var liArray=document.document.querySelectorAll("#sliderMenu li");
    for(var i=0;i<liArray.length;i++){
        var li=liArray[i];
        //set the href of all to #
        li.setAttribute('href',"#");
    }
</script>