简体   繁体   English

jQuery - 追加和删除HTML代码

[英]jQuery - Append and Remove HTML code

In jQuery, how can I append a line of HTML to some HTML, as well as remove the line of HTML? 在jQuery中,如何将HTML行添加到某些HTML中,以及删除HTML行?

Here is the before code: 这是前面的代码:

<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>

Here is the after code: 这是后面的代码:

<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>

I am needing to add this line of code: 我需要添加这行代码:

<span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>

Also, the reverse. 另外,相反。 How can I remove this line of code? 如何删除这行代码?

Here is the before code: 这是前面的代码:

<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>

Here is the after code: 这是后面的代码:

<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>

Sorry, I forgot to mention that I am a beginner when it comes to jQuery. 对不起,我忘了提到我是jQuery的初学者。 I am looking at the append and remove functions right now. 我正在查看追加和删除功能。 Thanks in advance. 提前致谢。

只需使用.append().remove()方法。

It would be more appropriate if you use show() and hide() for an error message like 如果你使用show()和hide()来表示错误信息会更合适

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

for showing the message and 用于显示消息和

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

for hiding it, use if else for checking your condition. 隐藏它,使用if else来检查你的状况。

Providing that you know input field element that you want to show/hide validation messages for, you can use after method to add message: 如果您知道要显示/隐藏验证消息的输入字段元素,则可以使用after方法添加消息:

$field.after('<span class="field-validation-error" data-valmsg-for="txtCvc" data-valmsg-replace="true">Test message.</span>');

and remove to remove it: remove以删除它:

$field.next('.field-validation-error').remove();

So $field can be something like this: 所以$field可以是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM