Javascript 按Id读取元素

Javascript 按Id读取元素,javascript,jquery,html,Javascript,Jquery,Html,嗨,我有下表 <table> <tr id=""> <td class="row_class" id="row_id_1">233.00</td> <td class="row_class" id="row_id_2">2300</td> <td class="row_class" id="row_id_3">3.00</td> <td cl

嗨,我有下表

<table>
   <tr id="">
      <td class="row_class" id="row_id_1">233.00</td>
      <td class="row_class" id="row_id_2">2300</td>
      <td class="row_class" id="row_id_3">3.00</td>
      <td class="row_class" id="row_id_4">4.00</td>
      <td class="main" id="row_id_5">5.00</td>
      <td class="main" id="row_id_6">112.00</td>
   </tr>
</table>
当我运行它时,我得到以下错误

TypeError: document.getElementById(...) is null
var num = document.getElementById('table td .totalS').value;

我正在试图找出修复此错误的方法。我不确定是否需要应用。每个函数是否要将格式应用于所有元素?

试试看,您正在尝试使用
文档获取值。getElementById
className
一起使用,这是错误的

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
    }
在Jquery中

   function formatCurrency() {
        alert();
        var num = $('#row_id_1').val();
        var cNum = $('#row_id_5').val();
    }

尝试此操作,您试图使用
document.getElementById
className
获取值,这是错误的

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
    }
在Jquery中

   function formatCurrency() {
        alert();
        var num = $('#row_id_1').val();
        var cNum = $('#row_id_5').val();
    }

尝试此操作,您试图使用
document.getElementById
className
获取值,这是错误的

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
    }
在Jquery中

   function formatCurrency() {
        alert();
        var num = $('#row_id_1').val();
        var cNum = $('#row_id_5').val();
    }

尝试此操作,您试图使用
document.getElementById
className
获取值,这是错误的

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
    }
在Jquery中

   function formatCurrency() {
        alert();
        var num = $('#row_id_1').val();
        var cNum = $('#row_id_5').val();
    }
试试这个:

 function formatCurrency() {
            alert();
            var num = $('table td.row_class').html();
            var cNum = $('table td.main').html();
            document.getElementById(val1).value = processformat(num);
            document.getElementById(val2).value = processformat(cNum);
        }
试试这个:

 function formatCurrency() {
            alert();
            var num = $('table td.row_class').html();
            var cNum = $('table td.main').html();
            document.getElementById(val1).value = processformat(num);
            document.getElementById(val2).value = processformat(cNum);
        }
试试这个:

 function formatCurrency() {
            alert();
            var num = $('table td.row_class').html();
            var cNum = $('table td.main').html();
            document.getElementById(val1).value = processformat(num);
            document.getElementById(val2).value = processformat(cNum);
        }
试试这个:

 function formatCurrency() {
            alert();
            var num = $('table td.row_class').html();
            var cNum = $('table td.main').html();
            document.getElementById(val1).value = processformat(num);
            document.getElementById(val2).value = processformat(cNum);
        }

您必须了解,您的代码正在尝试访问一个html标记,该标记被赋予在“getElementById()”参数中定义的任何内容的“id”属性

var num = document.getElementById('table td .row_class').value;
上面的代码是这样的:获取一个id为“table td.row_class”的html标记的值,并将其放入“num”变量中,该变量当然返回null,因为您没有任何id为“table td.row_class”的标记

您需要解决两件事:

  • getElementById()参数,以及
  • innerHTML属性而不是value属性
您的解决方案如下所示:

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
        document.getElementById('row_id_1').innerHTML= processformat(num);
        document.getElementById('row_id_5').innerHTML= processformat(cNum);
    }

您必须了解,您的代码正在尝试访问一个html标记,该标记被赋予在“getElementById()”参数中定义的任何内容的“id”属性

var num = document.getElementById('table td .row_class').value;
上面的代码是这样的:获取一个id为“table td.row_class”的html标记的值,并将其放入“num”变量中,该变量当然返回null,因为您没有任何id为“table td.row_class”的标记

您需要解决两件事:

  • getElementById()参数,以及
  • innerHTML属性而不是value属性
您的解决方案如下所示:

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
        document.getElementById('row_id_1').innerHTML= processformat(num);
        document.getElementById('row_id_5').innerHTML= processformat(cNum);
    }

您必须了解,您的代码正在尝试访问一个html标记,该标记被赋予在“getElementById()”参数中定义的任何内容的“id”属性

var num = document.getElementById('table td .row_class').value;
上面的代码是这样的:获取一个id为“table td.row_class”的html标记的值,并将其放入“num”变量中,该变量当然返回null,因为您没有任何id为“table td.row_class”的标记

您需要解决两件事:

  • getElementById()参数,以及
  • innerHTML属性而不是value属性
您的解决方案如下所示:

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
        document.getElementById('row_id_1').innerHTML= processformat(num);
        document.getElementById('row_id_5').innerHTML= processformat(cNum);
    }

您必须了解,您的代码正在尝试访问一个html标记,该标记被赋予在“getElementById()”参数中定义的任何内容的“id”属性

var num = document.getElementById('table td .row_class').value;
上面的代码是这样的:获取一个id为“table td.row_class”的html标记的值,并将其放入“num”变量中,该变量当然返回null,因为您没有任何id为“table td.row_class”的标记

您需要解决两件事:

  • getElementById()参数,以及
  • innerHTML属性而不是value属性
您的解决方案如下所示:

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
        document.getElementById('row_id_1').innerHTML= processformat(num);
        document.getElementById('row_id_5').innerHTML= processformat(cNum);
    }

所以什么是
val1
然后什么是
val2
表td。row_class不是
id
,你可以看看什么是
val1
,然后什么是
val2
表td。row_class不是
id
,你可以先看什么是
val1
,然后看什么是
val2
表td。row_class不是
id
,你可以先看什么是
val1
,然后看什么是
val2
表td。row_class不是
id
,你可以看一下