简体   繁体   English

使用JQuery插入HTML字符实体

[英]Inserting HTML character entities with JQuery

I'm trying to create a tool that allows me to insert different Spanish characters into a text box such as the inverted question mark. 我正在尝试创建一个工具,允许我将不同的西班牙字符插入文本框(例如倒置的问号)中。 I've currently got the following, but this returns: 我目前有以下内容,但是返回:

function (e){var n,r,i,o=this[0];{if(arguments.length)return i=x.isFunction(e),this.each(function(n){var o;1===this.nodeType&&(o=i?e.call(this,n,x(this).val()):e,null==o?o="":"number"==typeof o?o+="":x.isArray(o)&&(o=x.map(o,function(e){return null==e?"":e+""})),r=x.valHooks[this.type]||x.valHooks[this.nodeName.toLowerCase()],r&&"set"in r&&r.set(this,o,"value")!==t||(this.value=o))});if(o)return r=x.valHooks[o.type]||x.valHooks[o.nodeName.toLowerCase()],r&&"get"in r&&(n=r.get(o,"value"))!==t?n:(n=o.value,"string"==typeof n?n.replace(V,""):null==n?"":n)}}¿

HTML HTML

<div class="container">
<textarea id="text"></textarea>
<div id="one">&#191;</div>

JS JS

$('document').ready(function() {
$('#one').click(function() {
    $('#text').val($('#text').val + '&#191;');
});
});

Any ideas on why I'm receiving this output? 关于我为什么收到此输出的任何想法? Also do HTML entities work within textboxes, if not how would I do so? HTML实体也可以在文本框中工作吗,如果没有,我该怎么做? No errors appear in the console. 控制台中没有错误出现。

JSFiddle 的jsfiddle

$("#text").val is a function, which you are supposed to call. $("#text").val是一个您应该调用的函数。

That said, jQuery is massive overkill for such a trivial task: 也就是说,jQuery对于这种琐碎的任务来说是过大的杀伤力:

document.getElementById('one').onclick = function() {
    document.getElementById('text').value += "\u00bf";
}

Note that \¿ is the appropriate JavaScript escape sequence for ¿ - bf being 191 in hexadecimal. 需要注意的是\¿是¿相应的JavaScript转义序列- bf191十六进制。

EDIT: Alternatively, just hold Alt and type 0191 on your keypad. 编辑:或者,只需按住Alt并在键盘上键入0191。 This will produce ¿ directly where you are typing, instead of at the end of the input which is what your JS does. 这将直接在您键入的位置生成¿,而不是在JS所做的输入末尾生成。

Try this one. 试试这个。 Worked in your fiddle. 在你的小提琴中工作。

$(function() {
    $('#one').on('click', function() {
        $('#text').val($('#text').val() + "¿");
    });
});

Update: I posted spanish sign of question instead of its ASCII, changed $(document).ready to a shorter way $(function() , and added brackets after val() . 更新:我张贴了西班牙的问题符号,而不是其ASCII,将$(document).ready更改为较短的方式$(function() ,并在val()之后添加了括号。

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

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