简体   繁体   English

如何在动态创建的文本框中添加水印?

[英]How to add watermark to dynamically created text boxes?

I have a web page where I get a count of total Text box to be added dynamically. 我有一个网页,我可以动态添加一个总文本框的计数。 I have added a HTML table to which I add rows and cells to place the text box. 我添加了一个HTML表格,我在其中添加行和单元格以放置文本框。

I do not wish to add label controls for each text box but wish to place watermark text inside each text box which are dynamically created as an array of text boxes. 我不希望为每个文本框添加标签控件,但希望在每个文本框中放置水印文本,这些文本框是动态创建的文本框数组。

Please help me to achieve this. 请帮我实现这个目标。 Any code will be much better! 任何代码都会好得多!

Below code is what I have written which only created text box inside the HTML table control. 下面的代码是我写的,只在HTML表格控件中创建了文本框。

//Add row to table
tRow = new TableRow();
tblBokingDet.Rows.Add(tRow);

//Add cell to row
tCell = new TableCell();
tRow.Cells.Add(tCell);

//Add a literal text as control
AddTextBox = new TextBox();
AddTextBox.ID = "txtName" + i;                    
tCell.Controls.Add(AddTextBox);

I don't know if there's a way to wire up something via the Ajax toolkit to dynamically created textboxes on the fly, but an "open" way of doing it would be to use JavaScript to attach watermarks to each of your text boxes. 我不知道是否有办法通过Ajax工具包连接到动态创建的文本框,但是“开放”的方式是使用JavaScript将水印附加到每个文本框。 (I say "open" because its generally good practice to separate your UI functionality from whatever backend framework you're using for portability/reuseability.) (我说“开放”是因为通常很好的做法是将UI功能与您用于可移植性/可重用性的任何后端框架分开。)

Here's the jQuery way . 这是jQuery的方式

Here's the vanilla javascript way 这是vanilla javascript方式

You're hooking into the onfocus() and onblur() events of each textbox. 您正在连接每个文本框的onfocus()和onblur()事件。 Of course, you'll need to create a js function generic enough to iterate through each of your textboxes and wire up their events. 当然,您需要创建一个足够通用的js函数来迭代每个文本框并连接它们的事件。

Sample (for single textbox): 示例(单个文本框):

<input id="MyTextBox" type="text" value="Search" />

<script>
    var MyTextBox = document.getElementById("MyTextBox");

    MyTextBox.addEventListener("focus", function(){
        if (this.value=='Search') this.value = '';
    }, false);

    MyTextBox.addEventListener("blur", function(){
        if (this.value=='') this.value = 'Search';
    }, false);
</script>

Try : 试试:

        AddTextBox = new TextBox();
        AddTextBox.ID = "txtName" + i;                    
        tCell.Controls.Add(AddTextBox); // add to the cell
        AddTextBox.Attributes.Add("class", "hintTextbox");
        AddTextBox.Attributes.Add("onfocus", "if (this.value=='Name') this.value = '';this.style.color = '#000';");
        AddTextBox.Attributes.Add("onblur", "if (this.value=='') {this.value = 'Name';this.style.color ='#888';}");

Add the CSS: 添加CSS:

         INPUT.hintTextbox       { color: #888; }
         INPUT.hintTextboxActive { color: #000; }​

See sample fiddle here : http://jsfiddle.net/F5R6Z/3/ 请看这里的示例小提琴: http//jsfiddle.net/F5R6Z/3/

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

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