JS简单数学

时间:2017-03-30 06:44:42

标签: javascript jquery

你好,我需要一个快速的手,我有下面的这个div

<div class="price">$1500</div>

我需要做一些简单的数学计算,我需要将它除以一半,然后除以6并显示如下结果。

<span class="payment">Pay as low as $"result here" </span>

我是一名平面设计师,我需要将其用于网页,我知道这应该很简单,但我无法理解,我非常感谢这里的任何帮助。

7 个答案:

答案 0 :(得分:2)

首先获取div var text = $('.price').text(); // "$1500"的文本 然后将其解析为整数var price = text.replace(/[^\d]/, ''); // "$1500" -> 1500
最后将文本放在它所属的位置:$('.payment').text('Pay as low as $'+(price / 2 / 6));

&#13;
&#13;
$(document).ready(function () {
  var text = $('.price').text();
  var price = text.replace(/[^\d]/, '');
  $('.payment span').text(price / 12);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="price">$1500</div>
<span class="payment">Pay as low as $<span></span> </span>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在这里,这个是纯粹的javascript,尽量不要过度复杂化

&#13;
&#13;
<html>
<body>

    <div id="num">1500$</div>
    <div id="num2"></div>

    <script>
        var number = document.getElementById("num").innerHTML.replace("$", "");
        number = number / (2 * 6);
        document.getElementById("num2").innerHTML = "Pay as low as " + number + " here";
    </script>

</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

  
      
  1. 获取元素的navigator.getWebcam = (navigator.getUserMedia || navigator.webKitGetUserMedia || navigator.moxGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); if (navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ audio: true, video: true }) .then(function (stream) { //Display the video stream in the video object }) .catch(function (e) { logError(e.name + ": " + e.message); }); } else { navigator.getWebcam({ audio: true, video: true }, function (stream) { //Display the video stream in the video object }, function () { logError("Web cam is not accessible."); }); }
  2.   
  3. 替换text()标志
  4.   
  5. 除以$
  6.   
  7. 使用一些(2*6)函数,它将计算结果
  8.   
  9. 然后应用Math.abs并附加到您的身体
  10.   

&#13;
&#13;
span
&#13;
var a = Math.abs(parseInt($('.price').text().replace('$',''))/(2*6))

$('body').append('<span class="payment">Pay as low as $'+a+' </span>')
&#13;
.payment{
color:green;

}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

试试这个

var price = parseInt( $( ".price" ).html().replace(/\D/g, "") );
$( ".payment" ).html( "Pay as low as " + (price/12) );

<强>演示

  var price = parseInt( $( ".price" ).html().replace(/\D/g, "") );
    $( ".payment" ).html( "Pay as low as " + (price/12) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="price">$1500</div>
<span class="payment">Pay as low as $"result here" </span>

答案 4 :(得分:0)

你应该做什么:

  1. 获取.price div的文字。
  2. 将其转换为int
  3. 现在您应该将其除以(value/2)/6
  4. 将值放在所需的目标元素中。
  5. &#13;
    &#13;
    var price = +$('.price').text().slice(1);
    var pay = (price/2)/6
    $('p').text("Pay as low as $"+pay);
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="price">$1500</div>
    <p></p>
    &#13;
    &#13;
    &#13;

答案 5 :(得分:0)

您可以按照以下方式实现此目的。

$(document).ready(function(){
 var price = $(".price").text(),
     price = price.replace('$', '');
     finalPrice = (price/2).toFixed(2);

$(".final-price").text(finalPrice/6);
  
 
    

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="price">$1500</div>
<br> <br>

<div class="payment">Pay as low as $<span class="final-price"></span> </div>

答案 6 :(得分:0)

希望它有所帮助。

	/*define a function to Deleting all characters other than numbers. in element*/
  function checkIsInteger(sTr){
		var newstr = "";
		if(typeof sTr !== "undefine" && sTr!=""){
			var indexInt = ['0','1','2','3','4','5','6','7','8','9'];
			for(var i=0;i<sTr.length;i++){
				var cek = sTr.substr(i,1)
				if(indexInt.indexOf(cek)>=0){
					newstr += sTr.substr(i,1);
				}
			}
		}
		return newstr;
	}

$("#res").html((parseInt(checkIsInteger($("#price").html()))/2)/6);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="price" id="price">$1500</div>
<label class="payment" id="pym">Pay as low as $<span id="res">result</span> here </label>