English 中文(简体)
如何添加从 html 表格中取出的 html 内容?
原标题:How to add html contents taken from an html table?

我有这个剧本

$( table#preview td.price ).each(function(){
var price = parseInt($(this).html());
var total;
if(price ==   ){
price = 0;
}
total += price;
alert(total);
});

它所做的就是得到一列 持有股价 然后据推测加起来所有。

然而,我从这个代码得到的只是NAN, 我不明白代码有什么问题。

请注意脚本 < enger > if (价格 =) < / strong > 。 我这样做是因为表格中最初没有内容。

编辑: 这里 html

    <table>
    <tr>
    <th>Name</th>
    <th>Price</th>
    </tr>
    <tr>
    <td>pen</td>
    <td class="price>6</td>
    <td>paper</td>
    <td class="price>8</td>    
    </tr>
    </table>
最佳回答

Try 使用 .text () 方法,而不是 .html () 方法,希望能帮助消除 NaNN 错误。 您会想要在每次迭代范围外声明 common , 这样它不会每次被重置。 尝试一次重置, 我略微简化它, 以包含一个对数字 price 的检查 :

var total = 0;
$( table#preview td.price ).each(function()
{
    var price = parseInt($(this).text());
    if (!isNaN(price))
    {
        total += price;
    }
});

alert( The total is:   + total);

Update

s 这里的""http://jsfiddle.net/5jAak/" rel="noreferrer" >jsFiddle .N.B. .html() 也应该有效。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签