使用jQuery .each()读取任何html值并更改它
Using jQuery .each() to read any html value and change it
我想使用jQuery函数。each()来应用一个可能的改变值
我有一堆标签在标签class="temp"。如果我有一个切换摄氏度和华氏度之间的切换,我想更新所有的html元素与temp类。我将从span中检索值,然后更新span以获得新的温度。
Span标签看起来像
<span class="temp" id="holder_1443531366" style="text-align:center; display:inline-block;"><span style>86.1</span>
<span style="padding-left:3px;">°F</span>
</span>
我确定这不是最好看的html元素,我没有做,我用一个网站来建立一个web仪表板,实际上不能直接改变这些
这段代码还有其他部分,但我只需要主要部分工作。
//Find all elements with span class="temp"
var temps = [];
var html;
var pHtml;
temps = jQuery('.temp').toArray();
html = jQuery('.temp').html();
//pHtml = jQuery('.temp').parseHTML();
if(temps != null){
console.log("Temps -> "+temps.length);
console.log("html -> "+html);
for(var i = 0; i < temps.length; i++){
console.log(temps[i]);
}
}else {
console.log("No temps found");
}
我目前有两个元素要查找,这是到目前为止的输出
Attempting to find temp classes
Temps -> 2
html -> <span style="">86.0</span><span style="padding-left:3px;">°F</span>
<span class="temp" id="holder_1443531366" style="text-align:center; display:inline-block;"><span style>86.1</span><span style="padding-left:3px;">°F</span></span>
<span class="temp" id="holder_1443531376" style="text-align:center; display:inline-block;"><span style>85.38</span><span style="padding-left:3px;">°F</span></span>
我用"。类,找到Temp(86.1),然后使用该值执行我的Temp转换函数(或者只是一些数学运算)。有了这个新值,我将把元素从86.1更新为刚才计算的摄氏温度。
这段代码的作用是,在点击链接时,它循环遍历具有.temp
类的每个元素并获得.degrees
,将其转换为摄氏度,然后将F更改为c。
我在两个跨度上添加了类来完成这个(.degrees
和.degType
)
$(document).ready(function() {
$('.toC').on('click', function() {
$('.temp').each(function() {
var degrees = $(this).find('.degrees').html();
var cTemp = (parseFloat(degrees - 32) * (parseFloat(5/9))).toFixed(1);
$(this).find('.degrees').html(cTemp);
$(this).find('.degType').html('°C');
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="temp" id="holder_1443531366" style="text-align:center; display:inline-block;"><span class="degrees">86.1</span>
<span class="degType" style="padding-left:3px;">°F</span>
</span>
<br/><br/>
<span class="temp" id="holder_1443531366" style="text-align:center; display:inline-block;"><span class="degrees">212</span>
<span class="degType" style="padding-left:3px;">°F</span>
</span>
<br/><br/>
<a class="toC" href="#">to Celcius<a/>
这是一个来回切换的方法
$('a').click(function () {
$('span.temp').each(function () {
if ($(this).find('span:last').text() == '°F') {
$(this).find('span:last').text('°C');
$(this).find('span:first').text(parseFloat((($(this).find('span:first').text() - 32) * 5) / 9).toFixed(2))
} else {
$(this).find('span:last').text('°F');
$(this).find('span:first').text(parseFloat((($(this).find('span:first').text() * 9) / 5) + 32).toFixed(2))
}
})
$(this).text($(this).text() == 'switch to Fahrenheit' ? 'switch to Celsius' : 'switch to Fahrenheit')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="temp" id="holder_1443531366" style="text-align:center; display:inline-block;">
<span style>86.1</span>
<span style="padding-left:3px;">°F</span>
</span>
<span class="temp" id="holder_1443531367" style="text-align:center; display:inline-block;">
<span style>212</span>
<span style="padding-left:3px;">°F</span>
</span>
<a href="#">switch to Celsius</a>
尝试使用.find()
来过滤第一个span
, .html(function(index, html){})
将华氏度转换为摄氏度,选择下一个span
将F
更改为C
$(".temp").each(function(index, el) {
$(this).find("span:first").html(function(_, temp) {
return ((temp - 32) * 5/9).toFixed(2)
}).next("span").html(function(_, sym) {
return sym.slice(0,1) + "C"
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<span class="temp" id="holder_1443531366" style= "text-align:center;display:inline-block;"><span style>86.1</span><span style="padding-left:3px;">°F</span></span>
<span class="temp" id="holder_1443531376" style= "text-align:center;display:inline-block;"><span style>85.38</span><span style="padding-left:3px;">°F</span></span>
我已经做了一个工作的例子,并测试了它的工作
http://jsfiddle.net/a2dax16t/我已经像您一样创建了一个示例集,但是添加了一个按钮来触发这个函数:<span class="temp" id="holder_1443531366" style="text-align:center;display:inline-block;">86.1</span>
<br/>
<span class="temp" id="holder_1443531366" style="text-align:center;display:inline-block;">99</span>
<br/>
<span class="temp" id="holder_1443531366" style="text-align:center;display:inline-block;">102</span>
</span>
<br/>
<button id="btnCelsius">Convert</button>
,下面是我的函数:
$(function () {
$("#btnCelsius").click(function () {
$(".temp").each(function () {
var newValue = $(this).html();
$(this).html(toCelsius(newValue));
});
});
});
function toCelsius(f) {
return (5 / 9) * (f - 32);
}
如您所见,我创建了一个函数来转换为摄氏温度,并在each()
函数中使用它,并对其进行了测试,它正在工作。
请试一试,如果这是你想要的,或者你需要任何额外的修改,请告诉我。
相关文章:
- HTML 5 和 3.js 代码不会在网页上显示任何内容
- jQuery选择器不识别任何动态创建的HTML输入函数
- 如何获取保存在变量中的HTML代码的任何属性的值
- 如何通过ajax php将图像上传到服务器本地目录,其中html表单haven't任何传统的提交按钮
- 如何在javascript中调用onclick事件?我没有'我不想在我的HTML页面中使用任何事件
- 如何使用 html 类或 id 在任何地方显示内容
- 使用HTML和任何服务器端语言(PHP,.net,ruby)制作的网站是否可以在每个具有浏览器的设备上工作
- 如何在不使用任何 html 输入/搜索元素的情况下读取本地客户端文本文件
- 当“不相关”的 html 选择元素没有选择任何选项时,如何退出 jQuery 事件
- 可以'在html中,没有任何东西可以停留在图片上方
- 有没有任何方法可以将json对象存储在HTML标记的默认/自定义属性中,比如<tr>
- 是否有任何Ruby工具或JavaScript-Lib来验证html的格式是否正确
- 将html元素定位在页面中的任何x/y坐标处
- 使用jQuery检查html元素中是否存在任何字符串
- 如何在URL中不指定.html、.json的情况下配置web.xml来服务任何请求
- 删除HTML脚本标记会对它所包含的JavaScript产生任何影响吗
- 是否有任何jquery可以检测html标记是否未关闭?或页面未完全加载
- 任何一个输入所需的文本区域或 HTML 表单中的复选框
- 正则表达式选择任何 html 标记之间的字符串
- 任何使 HTML 禁用复选框变暗的方法