大家好,我正在寻找下一个 <p>
接近这个并将其文本输出到同一页面内的另一个 div 中。问题是当我进行编码并将其输出给带有警报的用户时,它是空白的。下面是我的代码:
$(document).ready(function(){
$('a.lightbox').click(function(e){
var txt = $(this).closest("#hh-name").text()
alert(txt);
$('body').css('overflow-y',' hidden');
$('<div id="overlay"></div>')
.css('top', $(document).scrollTop())
.css('opacity', '0')
.animate({'opacity': '0.7'}, 'slow')
.appendTo('body');
$('<div id= "lightbox"></div>')
.hide()
.appendTo('body');
$('<img>', {
src: $(this).attr('href'),
load: function() {
positionLightboxImage();
},
click : function() {
removeLightbox();
}
}).appendTo('#lightbox');
return false;
});
});
function positionLightboxImage() {
var top = ($(window).height() - $('#lightbox').height()) / 2;
var left = ($(window).width() - $('#lightbox').width()) / 2;
$('#lightbox')
.css({
'top': top + $(document).scrollTop(),
'left': left,
'border': '7px solid white',
'border-radius': '4px'
})
.fadeIn();
}
function removeLightbox() {
$('#overlay, #lightbox')
.fadeOut('slow', function() {
$(this).remove();
$('body').css('overflow-y', 'auto'); // show scrollbars!
});
}
<a class="lightbox" href="<?php echo $row_searchHH['imageURL']; ?>"><img id="hh-image" src="<?php echo $row_searchHH['imageURL']; ?>" width="90" height="90" style="margin-left:10px" /></a></div></td>
<td style="word-wrap:break-word; font-size:20px; font-family:'Myriad Pro'; font-weight:500" width="334" height="35"><p id="hh-name"><?php echo $row_searchHH['name']; ?></p>
提前致谢
最佳答案
试试这个:http://jsfiddle.net/C576s/
使用最近的选择器不会为您返回任何东西(它不会寻找 sibling )。 我留下了调试警报,以便您将来知道如何检查此类内容。
$('a.lightbox').click(
function (e)
{
var $closest = $(this).next("#hh-name");
alert($closest.length);
var txt = $closest.text();
alert(txt);
$('<div id= "lightbox"></div>').appendTo('body');
$('<p>' + txt + '</p>').appendTo('#lightbox');
}
);
关于javascript - 选择下一个 <p id ="hg-name"> 中的文本并将其附加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204975/