javascript - 将 css 代码加载到 textarea 在最后一行添加 1
问题描述
我正在将我的 css 文件的内容加载到我的 textarea 中,但不知何故 textarea 无法正确显示它。第一行和最后一行还是不正确。
例如,我正在加载这样的内容:
.notifications {
position: fixed;
z-index: 9999;
}
...
.notifications > div {
position: relative;
margin: 5px 0px;
}
但是 textarea 像这样格式化它:
.notifications {
position: fixed;
z-index: 9999;
}
.notifications > div {
position: relative;
margin: 5px 0px;
}1
不知何故,第一行添加了很多空格,最后一行添加了“1”。如果我删除文本区域中的空格和“1”并保存它,我会发送以下“有效负载”:
"""
.notifications {\n
position: fixed;\n
z-index: 9999;\n
}\n
\n
/* Positioning */\n
.notifications.top-right {\n
right: 10px;\n
top: 25px;\n
}\n
\n
.notifications.top-left {\n
left: 10px;\n
top: 25px;\n
}\n
\n
.notifications.bottom-left {\n
left: 10px;\n
bottom: 25px;\n
}\n
\n
.notifications.bottom-right {\n
right: 10px;\n
bottom: 25px;\n
}\n
\n
/* Notification Element */\n
.notifications > div {\n
position: relative;\n
margin: 5px 0px;\n
}
"""
这是html:
<div class="form-group">
<textarea class="form-control" rows="10" class="js-public-css" name="public_css">
{{ include resource_path("/{$file}") }}
</textarea>
<input type="hidden" class="js-css-file-name" value="{{ $fileValue }}">
<button type="button" class="btn btn-success js-update-css-file-btn">
Speichern
</button>
</div>
这是js:
$('.js-update-css-file-btn').click(function () {
const content = $(this).prev().prev().html();
const file = $(this).prev(".js-css-file-name").val();
// console.log("content"+content);
// console.log("file"+file);
axios.post('brand/update', {
file: file,
content: content
})
.then((response) => {
})
.catch((error) => {
})
})
我不知道为什么会发生这种情况。
解决方案
您确实会在 textarea 中添加空格,因为textarea
HTML 中的标签之间确实有空格,因此您需要将它们彼此相邻放置 ( <textarea>...</textarea>
)。
您得到的 1 可能是因为您的 Axios 查询正在处理您的 CSS 内容,就像它是 JSON 一样。将您的查询格式标题设置为纯文本。
推荐阅读
- angular - 如何有条件地在 Angular 2+ 中包含第三方 JS(基于环境)
- batch-file - PuTTY psftp 从部分名称获取文件
- hibernate - 基于类的投影 (DTO) id 不工作,但基于接口的投影工作
- php - 使多个单元并排fpdf
- rest - REST API 的 Junit 测试用例
- wordpress - 在 /blog 下创建带有链接的 WordPress 帖子
- android - 当我添加工具时发生了什么:replace="android:appComponentFactory"
- ajax - MVC 控制器从 ajax 接收空值
- karate - 尝试在 KARATE DSL 中进行后调用时如何解决“错误 com.intuit.karate - http 请求失败:null”?
- python-3.x - python程序找出音频文件中每个单词的开始和结束时间