如何在Javascript中获取div id?

时间:2017-01-30 10:38:34

标签: javascript html css asp.net

我想点击li并点击属性。

<ul class="list-unstyled clearfix">
    <li style="float: left; width: 33.33333%; padding: 5px;" onclick="maviTema()">
</ul>

之后我将在javascript中运行maviTema函数。这个javascript代码更改了我的面板标题颜色。我该怎么办?

function maviTema() {
  var div = document.getElementById('baslikBilgi');           
  div.style.backgroundColor = 'green'; 
} 

我想设置颜色id =“baslikbilgi”:

<div class="bg-blue-active" id="baslikBilgi">Başlık Bilgileri</div>

5 个答案:

答案 0 :(得分:1)

我不确定你的问题是什么,因为你似乎已经在你的帖子中回答了这个问题。要使用vanilla JS更改元素的背景颜色,您需要做的就是设置<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="personcart">0</span> <span class="addtocartbt">addtocartbt</span>属性,如下所示:

backgroundColor

答案 1 :(得分:0)

为什么要通过点击触发功能,您可以在功能后立即调用它。

document.getElementById('baslikBilgi').style.backgroundColor='#0f0'
function maviTema() {
  var div = document.getElementById('baslikBilgi');
  div.style.backgroundColor = 'green';
}
maviTema();

  

或者如果设置bg-color只需要此功能,那么你可以试试   简单的代码

<div class="bg-blue-active" id="baslikBilgi">Başlık Bilgileri</div>

答案 2 :(得分:0)

<div class="bg-blue-active" id="baslikBilgi">Başlık Bilgileri</div>

<li style="float: left; width: 33.33333%; padding: 5px;" onclick="maviTema()">Link</li>

<script>
function maviTema() {
            var div = document.getElementById('baslikBilgi');
                div.style.backgroundColor = 'green';

        }
</script>

当我在index.html / index.php文件中有这个代码时,我点击了&#39; li&#39;它适用于我!

确保您在脚本标记中包含您的Javascript代码;)

答案 3 :(得分:0)

&#13;
&#13;
 
   function maviTema() {
        var div = document.getElementById('baslikBilgi');           
            div.style.background = 'green';

    } 
&#13;
<ul class="list-unstyled clearfix"> 

    <li style="float: left; width: 33.33333%; padding: 5px;"onclick="maviTema()">
    <a href="javascript:void(0);" data-skin="skin-blue" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover"> </a>
     <p class="text-center no-margin">Mavi</p> 
    </li>
    </ul>

<div class="bg-blue-active" id="baslikBilgi">Başlık Bilgileri</div>
&#13;
&#13;
&#13;

试试这个,

   <ul class="list-unstyled clearfix"> 

    <li style="float: left; width: 33.33333%; padding: 5px;"onclick="maviTema()">
    <a href="javascript:void(0);" data-skin="skin-blue" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover"> </a>
     <p class="text-center no-margin">Mavi</p> 
    </li>
    </ul>
 <script>
    function maviTema() {
        var div = document.getElementById('baslikBilgi');           
            div.style.background = 'green';

    } 
</script>
<div class="bg-blue-active" id="baslikBilgi">Başlık Bilgileri</div>

答案 4 :(得分:0)

您的准则运作正常。 确保您有脚本标记 如果要在页脚中将另一个js文件导入页面,请尝试将脚本移到它们上面或尝试从标题中运行它,它会正常工作

<script>

 function maviTema() {
 var div = document.getElementById('baslikBilgi');
            div.style.backgroundColor = 'green';

        }
</script>