使用JQuery为图像添加跨度
Adding span to images with JQuery
我试图使用mansory.js创建一个pinterest喜欢的网页,我有麻烦附加到每个图像的唯一描述。我知道我的代码的最后一行是错误的,但我不知道如何修复它。我基本上是将所有的描述标签添加到每个图像的一个span中。
我试着看看其他几个stackoverflow问题,如jQuery:添加元素后的另一个元素和添加图像/s内的div与jQuery但是运气不好。
我的整支笔都在这里http://codepen.io/OG/pen/VLEXgz
HTML<body>
<h1>Camper Stories</h1>
<div id="content"></div>
</body>
CSS h1{
text-align:center;
font-family: 'Lobster', cursive; font-size:80px;
color:purple;
}
#content {
width: auto;
margin: 0 auto;
}
.item {
display: inline-block;
float: left;
width: 300px;
height:300px;
margin: 2em;
position:relative;
}
.item img {
width: 300px;
height: auto;
}
JS
var url = 'http://www.freecodecamp.com/stories/hotStories';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {
var i;
var headlines = [];
//loop through json get need attributes
//for(i = 0; i < arr.length; i++){
for (i = 0; i < 5; i++) {
var headline = arr[i].headline;
headlines.push(headline);
var authorImg = arr[i].author.picture;
//console.log(img);
//error function
//if there is no thumbnail link the get author image
if (img === "") {
$('#content').append('<div class="item">' + '<a href="' + link + '">' + '<img id="myImg" src="' + authorImg + '" alt="' + headline + '" />' + '</a>' + '</div>');
} else {
//if there is a thumbnail image then use that image
$('#content').append('<div class="item" id="hi">' + '<a href="' + link + '">' + '<img id="myImg" src="' + img + '" alt="' + headline + '" />' + '</a>' + '</div>');
//if there is a 404 error with loading the thumbnail image then use author's image
$('img').one('error', function () {
this.src = '' + authorImg;
})
}
$(arr[i].headline).insertAfter("img");
}
}
见http://codepen.io/anon/pen/YXJvEK你是这个意思吗?只在相应的图片后面附加标题?
$("<span>" + arr[i].headline+"</span>").insertAfter($("img").eq(i));
你应该这样构建你的元素:
var wrapper = $("<div />");
var link = $("<href />");
var img = $("<img />");
...
然后添加所需的属性和类,并将它们相互追加。
link.append(img);
wrapper.append(link);
...
这样你的代码变得更容易维护,你可以很容易地把你的span添加到你的img(不知道你想要的描述)。
编辑,既然我有一台电脑,这里是你可以使用的代码。
function myFunc (data) {
if(typeof data === "undefined") return; // output some error
for (var i = 0; i < data.length; i++) {
var item = data[i];
var upVote = item.rank;
var href = item.link;
var image = (item.image === "") ? item.author.picture : item.image;
var headline = item.headline;
// build elements
var wrapper = $("<div />");
var link = $("<a />");
var img = $("<img />");
var description = $("<span />");
// add attributes and classes
wrapper.addClass("item");
link.attr("href", link);
// you can also set multiple attributes at once
img.attr({"src": image, "alt": headline});
description.text(headline); // text(string) adds string to element
// append elements onto each other
link.append(img);
wrapper.append(link, description);
$("div.content").append(wrapper); // attach elements to DOM
}
}
相关文章:
- jquery如何从计算机添加图像
- 如何从 URL 数组向文档添加图像列表
- 如何在fabric js中向多边形添加图像
- Bootstrap多选-在复选框后添加图像
- 通过纯JS添加图像
- 如何在鹅毛笔JS中添加图像
- 使用java脚本添加图像进行测验
- 如何添加可拖动的“;添加图像”;jquery中的图标
- 添加图像与点svg画布
- 是否可以添加图像作为徽标颜色框
- 如何使用fabric.js添加图像过滤器
- JavaScript-添加图像:React Native XCode
- Dropzone”;addedfile”;以程序方式添加图像时不会激发
- 在使用Jquery/Javascript单击下一个和上一个按钮时动态添加图像
- 用图像替换页面,而不是添加图像
- 如何获取动态添加图像的宽度和高度
- 如何在这个javascript中添加图像链接
- 尝试在画布中单击鼠标以添加图像
- 在剑道 UI 树视图项旁边添加图像不起作用
- 如何使用BEML或JS在全屏Brightcove智能播放器中添加图像叠加