使用javascript在HTML标记中选择字符串

时间:2014-02-03 13:14:33

标签: javascript html

我有一个类似的脚本:

    var h = document.getElementById('price');
    var pric= node.h;
    var total=parseInt(pric);

    var tot = 0;
    tot = total*2;
    document.getElementById('p').innerHTML=tot;

这个HTML代码

<div id='price'>4000</div>
<div id='p'></div>

脚本有什么问题吗?因为我有来自js提示的错误:

缺少基数参数

3 个答案:

答案 0 :(得分:1)

因为你在jQuery中标记了,试试这个:

var total = $("#price").html() // get the html
total = total * 2;             // process it
$("#p").html(total)            // put it back

实现更具语义性,更容易理解。

小提琴中的演示:http://jsfiddle.net/Ny9uW/

答案 1 :(得分:0)

你的问题在于parseInt。

parseInt(string,radix);

var total = parseInt(pric, 10);

答案 2 :(得分:0)

问题

  1. 删除node.h
  2. 使用parseInt(string, radix)将文本转换为整数
  3. 使用innerHTML属性
  4. 使用

    var price = document.getElementById('price');
    var total=parseInt(price.innerHTML, 10);
    var tot = 0;
    tot = total*2;
    document.getElementById('p').innerHTML=tot;
    

    DEMO

相关问题