简体   繁体   English

来自输入字段时如何通过javascript函数设置文本样式

[英]How to style text via a javascript function when coming from an input field

I have one input field that facilitates a person having a conversation but playing both roles in the convo. 我有一个输入字段,可帮助一个人进行对话,但在convo中扮演两个角色。 I want to get as close as I can to what its like to have a text conversation, but I cannot seem to sort out how to style the text when it comes through. 我想尽可能地接近进行文本对话的感觉,但是我似乎无法弄清楚文本通过时如何设置样式。

As of the moment, the user types the text and hits one of two buttons, each is loaded with the following function to pull the text, create a div, text node, append them and place in the page. 到目前为止,用户键入文本并单击两个按钮之一,每个按钮都加载了以下功能以拉出文本,创建div,文本节点,将其附加并放置在页面中。

I tried styling the initial input but that simply makes the input field styled, does not affect the actual output. 我尝试设置初始输入的样式,但这仅使输入字段具有样式,不会影响实际输出。

I tried adding style at each step of the way, to the variable I saved the input in, to the p, the div, the text node, and after placing it in the doc... each time the function failed. 我尝试在方法的每个步骤中将样式添加到将输入保存到的变量,p,div,文本节点中,然后将其放置到文档中……每当函数失败时,就添加样式。

I tried the attribute method and an innerhtml approach. 我尝试了属性方法和innerhtml方法。

What would work? 什么会起作用? At minimum I would love the function to bold and right align the text. 至少我希望该功能使文本加粗和右对齐。 Next best would be to append it with the contents of an ng-app so it says Me: (text here), then My future self: (text here)... which I sense would just involve a string set to a variable.. but setting x = {{name}} caused the function to fail.. 第二个最好的办法是将它附加到ng-app的内容中,这样它说我:(此处为文本),然后为我的未来自我:(此处为文本)...我感觉这将只涉及设置为变量的字符串。 。,但是设置x = {{name}}会导致函数失败。

I know theres a way to use firebug to understand these failures, but I am not quite understanding that yet. 我知道有一种方法可以使用Firebug来了解这些故障,但是我还不太了解。 Any suggestions? 有什么建议么?

<script>




function changeTextComment4(destination){ 
// to be modified from the above to change the location of the dump
// this function ADDS a comment from the comment field to the div w id comment near it... 

var userInput = document.getElementById('userInputS1').value;  
// get the input from the user

// 3 make the div a panel

var para = document.createElement("P");
// assignment of attributes

var t = document.createTextNode(userInput);
para.appendChild(t);  


// add comment area

// place the item
var destination = document.getElementById(destination)
destination.insertBefore(para, destination.firstChild);

document.getElementById('userInputS1').value = "";
document.getElementById('userInputS1').focus();}

</script>

you can add style by referring to the selector 您可以通过引用选择器来添加样式

#userInputS1{
    color : #F00;
}

暂无
暂无

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

相关问题 如何从通过 Javascript function 动态创建的输入字段访问表单输入? - How can I access form input from an input field which was created dynamically via a Javascript function? 当用户在输入字段中键入文本或将文本粘贴到其中时,如何触发javascript函数? - How can a javascript function be fired when a user types text into an input field or pastes text into it? Firefox不会将值从输入文本字段传递到javascript函数 - Firefox not passing values from an input text field to a javascript function 如果详细信息来自后端,如何在JSP中将input = text字段设为只读 - How to make the input=text field readonly in JSP if details are coming from the backend 使用来自外部 Javascript Websocket 的数据更新 HTML 输入字段 - update a HTML input field with the data coming from a external Javascript Websocket 如何在javascript函数中访问输入文本字段的值? - How to access the value of the input text field in javascript function? 如何在 JavaScript 中转义来自 PHP 的文本? - How to escape text coming from PHP in JavaScript? 当键入光标离开输入文本字段时如何触发功能? - How to trigger a function when the typing cursor leaves the input text field? HTML/Javascript - 如何将输入字段中的文本显示为 HTML 代码? - HTML/Javascript - How to display text from input field as HTML code? 如何通过按下javascript中的按钮将文本从输入字段移至另一个 - How to move a text from an input field to another by pressing a button in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM