如何在<label>标签中以文本元素形式渲染<body>标签?
</body> as Plain Text Inside a <label> Tag Got it, let's sort this out for you! The problem you're running into is that browsers automatically parse </body> as actual HTML syntax instead of displaying it as regular text. To make it show up correctly inside your <label> tag, you need to escape those special HTML characters so the browser treats them as text, not code.
Static HTML Solution
The simplest fix for static HTML is to replace the angle brackets with their corresponding HTML character entities:
- Replace
<with< - Replace
>with>
Here's your updated code with the escaped </body> tag:
<label for="message-text" class="col-form-label"> <strong>Copy and paste this code in Google Tag Manager as custom html or paste this code before </body> tag on your website page(s).</strong> </label>
When rendered, this will display exactly as you want: Copy and paste this code in Google Tag Manager as custom html or paste this code before tag on your website page(s).
Dynamic Content Solution (JavaScript)
If you're generating this label content dynamically with JavaScript, avoid using innerHTML (which parses HTML) and use textContent instead. This property automatically escapes special characters, so you can write </body> directly without worrying about entity codes:
// Get your label element const messageLabel = document.querySelector('label[for="message-text"]'); // Set the text content directly messageLabel.textContent = 'Copy and paste this code in Google Tag Manager as custom html or paste this code before </body> tag on your website page(s).'; // If you need to keep the <strong> formatting, you can create the strong element separately: const strongText = document.createElement('strong'); strongText.textContent = 'Copy and paste this code in Google Tag Manager as custom html or paste this code before </body> tag on your website page(s).'; messageLabel.appendChild(strongText);
That's all there is to it! Either escaping the entities in static HTML or using textContent for dynamic content will make </body> show up as plain text in your label.
内容的提问来源于stack exchange,提问作者Sireini

