Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-附加和删除HTML代码_Javascript_Jquery_Html_Append_Removechild - Fatal编程技术网

Javascript jQuery-附加和删除HTML代码

Javascript jQuery-附加和删除HTML代码,javascript,jquery,html,append,removechild,Javascript,Jquery,Html,Append,Removechild,在jQuery中,如何将一行HTML附加到某些HTML中,以及如何删除该行HTML 以下是代码: <div class="form-group"> <label class="control-label col-md-2" for="txtCvc">Cvc</label> <div class="col-md-10"> <input class="form-control text-box single-lin

在jQuery中,如何将一行HTML附加到某些HTML中,以及如何删除该行HTML

以下是代码:

<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
        <span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>
    </div>
</div>
下面是后面的代码:

<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
        <span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
    </div>
</div>
我需要添加这行代码:

<span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>
而且,情况正好相反。如何删除这行代码

以下是代码:

<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
        <span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>
    </div>
</div>
下面是后面的代码:

<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
        <span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="txtCvc">Cvc</label>
    <div class="col-md-10">
        <input class="form-control text-box single-line" id="txtCvc" name="Cvc" type="text" value="" />
    </div>
</div>
对不起,我忘了提到我是jQuery的初学者。我现在正在研究append和remove函数。
提前感谢。

只需使用和方法。

如果您使用“显示和隐藏”来显示错误消息,如

$('.field-validation-error').show();
用于显示消息和

$('.field-validation-error').hide();

要隐藏它,请使用if-else检查您的条件。

如果您知道要显示/隐藏验证消息的输入字段元素,则可以使用方法添加消息:

$field.after('<span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>');
所以$field可以是这样的:

var $field = $('#txtCvc');

到目前为止你试过什么?在哪种情况下,应该修改某些内容?不要试图回答自己的问题。是否要附加某些内容并删除某些内容?那么你有没有试着看看这个和那个?你在这里的朋友是快车,不是吗SO@JamieBarker嗯,事实上,这里有一些朋友,他们将为您编写所有代码。他们可能会说我们在帮助,但事实上他们鼓励老年退休金计划不要试图自己去做/学习/理解一些事情。@Regent是的,代表农民~_o