如何在jquery风格中这样做

时间:2012-01-31 08:48:06

标签: jquery css

我有这段代码:

<p><label for="mobtel">Enter mobile no.:</label><br />
<input id="mobtel" type="text" name="mobtel"/></p>

我使用jquery隐藏它:

$("label[for=mobtel],#mobtel").hide();

现在我想要显示一个新标签和新输入,如下所示:

<p><label for="mobtel">Enter verification no.:</label><br />
<input id="verification_no" type="text" name="verification_no"/></p>

如何以jquery风格执行此操作?

4 个答案:

答案 0 :(得分:1)

$("body").append('<p><label for="mobtel">Enter verification no.:</label><br />
<input id="verification_no" type="text" name="verification_no"/></p>');

http://jsfiddle.net/K89ZU/1/

答案 1 :(得分:1)

我只是在HTML中正常包含两者,然后根据需要隐藏/显示:

<p id="mobtelField">
    <label for="mobtel">Enter mobile no.:</label><br />
    <input id="mobtel" type="text" name="mobtel"/>
</p>
<p id="verificationNoField">
    <label for="verification_no">Enter verification no.:</label><br />
    <input id="verification_no" type="text" name="verification_no"/>
</p>

function showVerificationNo(){
    $('#mobtelField').hide();
    $('#verificationNoField').show();
}
function showMobtel(){
    $('#verificationNoField').hide();
    $('#mobtelField').show();
}

CSS:

#verificationNoField{
    display:none;
}

然后打电话使用:

showVerificationNo();

答案 2 :(得分:0)

$("p").append('<label for="mobtel">Enter verification no.:</label><br />
<input id="verification_no" type="text" name="verification_no"/>');

答案 3 :(得分:0)

最简单的方法是在HTML中添加一个不可见的元素:

<p><label for="mobtel">Enter mobile no.:</label><br />
<input id="mobtel" type="text" name="mobtel"/></p>
<input id="verification_no" type="text" name="verification_no" style="display:none;"/></p>

然后:

$("#icatmobtel").hide();
$("#verifion_no").show();
$("label[for=mobtel]").text("Enter verification no.:");
$("label[for=mobtel]").attr("for", "verifion_no");

或者,您也可以添加第二个标签并隐藏/显示它。

相关问题