Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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/69.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中为图像动态添加alt标记_Javascript_Jquery_Html_Asp.net - Fatal编程技术网

需要在javascript中为图像动态添加alt标记

需要在javascript中为图像动态添加alt标记,javascript,jquery,html,asp.net,Javascript,Jquery,Html,Asp.net,我有一个如下的J.script,我想动态地将alt标记添加到 图像(id=“consumer Header”)基于某些条件 我尝试过以下方法,比如给div赋予id并设置属性,但对我来说没有任何效果(consumerheader为null) window.onload=getStrings() HTML中图像的ID是consumerHeader,在Javascript中使用的是consumerHeader。这就是它不起作用的原因 function getStrings() {

我有一个如下的J.script,我想动态地将alt标记添加到 图像(id=“consumer Header”)基于某些条件

  • 我尝试过以下方法,比如给div赋予id并设置属性,但对我来说没有任何效果(consumerheader为null)

    window.onload=getStrings()

    
    

  • HTML中图像的ID是
    consumerHeader
    ,在Javascript中使用的是
    consumerHeader
    。这就是它不起作用的原因

    function getStrings()
    {                             
    var $consumerheader = document.getElementById("consumerheader");
    //                                                     ^ Here is the issue make this "h" to "H"
    $consumerheader.setAttribute("alt", "R- Insurance");     
    var test = document.getElementById("consumerHeader").alt;
    alert("test");
    
    //var $img = $divconsumerHeader.getElementsByTagName("img")[0];   
    
    //control.setAttribute("consumerheader","R- Insurance")
    // var test1 = document.getElementsByTagName("img")[0];
    // alert("test1");
     } 
    
    注意:Javascript区分大小写。

    您是否厌倦了在文档加载时而不是在函数中调用函数?如果
    window.onload=getStrings()
    是脚本的一部分,它需要在创建函数之后而不是之前调用。
    <div id="divconsumerHeader" class="head" style="text-align: center;">
                    <img src="shared/images/headers/ConsumerOnline.png" id="consumerHeader" alt="" style="width: 177" />
    </div>       
    
    function getStrings()
    {                             
    var $consumerheader = document.getElementById("consumerheader");
    //                                                     ^ Here is the issue make this "h" to "H"
    $consumerheader.setAttribute("alt", "R- Insurance");     
    var test = document.getElementById("consumerHeader").alt;
    alert("test");
    
    //var $img = $divconsumerHeader.getElementsByTagName("img")[0];   
    
    //control.setAttribute("consumerheader","R- Insurance")
    // var test1 = document.getElementsByTagName("img")[0];
    // alert("test1");
     }