【问题标题】:Use html() and replaceWith() together一起使用 html() 和 replaceWith()
【发布时间】:2014-01-27 04:06:17
【问题描述】:

我想用另一个 div 替换一个 div,同时我想用从第一个 div 读取的值填充第二个 div

所以我猜我应该同时使用两者,但它不起作用。 这是我正在使用的代码:

$("#test1").replaceWith($("#register p").html(programType).show()); 

寄存器的div是:

<div id="register" style="display: none;">
    <div class="row">
        <div class="span4">
            <div class="field row">
                <div class="fieldName span0">Program : </div>
                <p>{{programType}}</p>
            </div>
        </div>
    </div>
</div>

所以不是得到:Program : Value of programType 我只得到:程序类型的值

【问题讨论】:

    标签: jquery html replacewith


    【解决方案1】:

    您正在引用寄存器中的 p 标记。为您接收 programType 的值

    【讨论】:

    • 即使我删除了对 p 的引用,我也会得到相同的结果
    • 要获得“程序”,您必须引用类“fieldName span0”。 “#register p”为您提供“价值...”
    • 我试过这个: $("#test1").replaceWith($("#register fieldName").html(programType).show());我尝试使用 .fieldName ,结果相同
    【解决方案2】:

    你可能想要类似的东西

    $("#test1").replaceWith($("#register")).show().find("p").html(programType);
    

    不清楚您要执行什么,因为不清楚#test1 是什么以及“而不是获取:程序:programType 的值我只得到:programType 的值”是什么意思。

    【讨论】:

    • #test1 是我可以选择程序类型的 div,在单击继续按钮时,我希望将其替换为 #register div ,我可以在其中看到用户选择的程序名称
    • 它应该看起来像 - 程序:programType 的值,但我只能看到缺少标签的 programType 的值
    • 实际检查示例然后在发现第二个有效时投票并接受怎么样?
    • 这两个例子我都试过了,先生!他们都没有工作。第一个不是我想要的,第二个不返回任何东西
    • 是的,我知道第一个不是您要求的(我可以将其编辑掉)......不过,第二个不起作用的事实很奇怪;但是您应该看到问题出在哪里:您显示替换为整个#register(代码所做的)并仅填充子部分“p”(find 之后为您执行)。即使没有替换内部,它也应该至少显示“程序:{{programType}}”。你更准确地说你想要什么:“我希望将#register复制到#test1中,并将其内部p替换为programType的值”......是这样吗?
    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多