javascript-根据td元素.text()值,使用css背景图像的存储值访问对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-根据td元素.text()值,使用css背景图像的存储值访问对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2760字,纯文字阅读大概需要4分钟。
内容图文
![javascript-根据td元素.text()值,使用css背景图像的存储值访问对象](/upload/InfoBanner/zyjiaocheng/685/76d656266abc436082f7bac779f2eafb.jpg)
我检查了其他答案,但没有发现任何相似之处.我不确定我的方法是否可行.
我有一个CRUD应用程序,所有的PHP和SQL代码都可以正常工作.
在用户输入时,它获取所查询食物的食物值.
食物名称放在td元素中.
我想要的是基于该td元素.text()方法的jQuery值添加背景图像.
这是HTML,都是在服务器端生成的:
<tr>
<th>#</th>
<th>Food Name</th>
<th>Calories/100g</th>
<th>Proteins</th>
<th>Carbohydrates</th>
<th>Fats</th>
<th>Time_to_burn_by_running</th>
</tr>
我得到的td元素的当前值是这样的:
var currentFood = $('td:eq( 1 )').text(); //cache it
alert(currentFood); // test it, works fine
我将背景图像存储在此对象中:
var imgSources = {
almond: "https://www.vigon.com/wp-content/uploads/2015/04/almonds.jpg",
banana: "https://www.organicfacts.net/wp-content/uploads/2013/05/Banana3-
1020x765.jpg",
cereal: "http://nutryvidasaude.com.br/Imagens/Produtos/240420170917331.jpg",
};
现在,我显然可以对函数中的每种食物选择和硬编码进行if评估,但这并不可行,并且违反了所有DRY原则.
这是我尝试的方法,我不确定将这样的事情连接起来是否可以.
$(function(){
if(jQuery){
/* alert("i am here"); */
}
if($('table').length == 0) {
alert("table is not generated yet");
}
else {
$('table').prop('id', 'mainTable');
var reso = $('#mainTable').attr('id')
alert("table id is "+ reso);
// this is the relevant part
var currentFood = $('td:eq( 1 )').text();
alert(currentFood);
alert(imgSources.cereal); // testing what is returned.
if($('table').length > 0) { $('#mainTable').fadeOut(100, function() {
$('#mainTable').fadeIn(2000);
// this one does not work
$('#mainTable').css('background-image', 'imgSources + "." +
currentFood').fadeIn(3000);
});}
}
});
控制台中没有错误消息.这只是串联问题吗?
如果我做 :
$('#mainTable').css('background-image', 'someurl').fadeIn(3000);
然后就可以了.
我正在服务器上运行它,我希望它足够好了,不用小提琴/ codepen
解决方法:
问题是因为imgSources是一个变量,所以您不能将其包括在字符串中-您需要将其与要访问的属性一起进行连接.
由于将属性名称存储在currentFood变量中,因此您还需要使用括号符号来访问对象.尝试这个:
var imgSources = {
almond: "https://www.vigon.com/wp-content/uploads/2015/04/almonds.jpg",
banana: "https://www.organicfacts.net/wp-content/uploads/2013/05/Banana3-1020x765.jpg",
cereal: "http://nutryvidasaude.com.br/Imagens/Produtos/240420170917331.jpg",
};
var currentFood = $('#mainTable td:eq(1)').text();
$('#mainTable').css('background-image', 'url(' + imgSources[currentFood] + ')').fadeIn(3000)
#mainTable {
height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mainTable">
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Calories/100g</th>
<th>Proteins</th>
<th>Carbohydrates</th>
<th>Fats</th>
<th>Time_to_burn_by_running</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>almond</td>
<td>550</td>
<td>18</td>
<td>14</td>
<td>48</td>
<td>55 </td>
</tr>
</tbody>
</table>
内容总结
以上是互联网集市为您收集整理的javascript-根据td元素.text()值,使用css背景图像的存储值访问对象全部内容,希望文章能够帮你解决javascript-根据td元素.text()值,使用css背景图像的存储值访问对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。