对所有子输入JQuery使用replaceWith

using replaceWith on all child inputs JQuery

本文关键字:JQuery 使用 replaceWith 输入      更新时间:2023-09-26

基本上在.show()上,我一直在尝试将所有输入转换为图像标签,img src等于原始输入值,如下所示:

var currentPage = $('.three_paj_els:visible');
var nextPage = currentPage.next('.three_paj_els');
var the_parent_div_id = currentPage.attr('id');
    nextPage.show(function() {
        $('div#' + the_parent_div_id + ':input').each(function() {
            var the_image_SRC = $(this).val();
            $(this).replaceWith('<img src="' + the_image_SRC + '" ')
        })
    })

我已经干了几个小时了。我只想让显示的特定div中的那些进行转换。

这是我一直在做的工作http://jsfiddle.net/Utr6v/100/单击"下一步"按钮时,<input type="hidden" />标记应转换为<img>标记,并显示图像。

提前表示感谢。-Sal

currentPage似乎没有ID。但你把它搞得太复杂了——如果你有元素,你可以用它来执行上的jQuery函数。你不需要进行元素->ID->元素转换,因为这毫无意义。

要查找子体,您需要在元素选择器和子体选择器之间放置一个空格,否则选择器将应用于元素本身。在您的情况下,您可以使用.find

此外,您还缺少图像的结束标记。

http://jsfiddle.net/Utr6v/101/

// I guess you want to replace with images on the new page, not the one
// which gets hidden
nextPage.find(':input').each(function() {
    var the_image_SRC = $(this).val();
    $(this).replaceWith('<img src="' + the_image_SRC + '">')
});