简体   繁体   English

JavaScript - div 的值大于,所以做点什么

[英]JavaScript - Value of div is greater than, so do something

When price (value) in div .highlight dd is greater than 750 , insert element from var somewhere当 div .highlight dd中的 price(value) 大于750时,从var某处插入元素

var CustomOrderText = "<div class='CustomOrderText'>XY</div>";

if there are additional questions, I will be happy to specify.如果还有其他问题,我很乐意具体说明。 Please do not delete this post (as before).请不要删除这篇文章(和以前一样)。 This is a very important question for me.这对我来说是一个非常重要的问题。

Edit: *I can´t remove "HTML entity" for space from the price, because I can't interfere in the generation process of the application that makes up HTML编辑:*我不能从价格中删除空间的“HTML 实体”,因为我不能干涉构成 HTML 的应用程序的生成过程

 var CustomOrderText = "<div class='CustomOrderText'>XY</div>"; $(CustomOrderText).insertBefore("div.target");
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <dl class="sumPrice withVat highlight "> <dd>779,70&nbsp;Kč</dd> </dl>

The first problem of your issue - this is a string 779,70 Kč (Not number).您问题的第一个问题 - 这是一个字符串779,70 Kč (不是数字)。 No way to find numbers that is greater than "hello" for example - one way is to use regex (Very modular).例如,无法找到大于“hello”的数字 - 一种方法是使用正则表达式(非常模块化)。 Related Q: How to find a number in a string using JavaScript?相关问题: 如何使用JavaScript查找字符串中的数字? . .

Less modular idea is to use replace() .较少模块化的想法是使用replace()

commas to your thousands (the string manipulation should be different): How can I parse a string with a comma thousand separator to a number?千位逗号(字符串操作应该不同): 如何将带有逗号千位分隔符的字符串解析为数字?

 var number_with_commas = '1,125'; number_without_commas = number_with_commas.replace(/,/g, ''); console.log(number_without_commas); /* jquery example */ var price = $(".price").text().replace(/,/g, '.').replace(/Kč/g, ''); console.log(price); if(price > 750){ console.log("price is higher than 750 - do something"); }
 <p><span class="price">779,70&nbsp;Kč</span></p> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM