愚蠢的转义引号问题

发布于 2024-12-02 03:39:48 字数 922 浏览 0 评论 0原文

我知道这应该是基本的,但对于我的一生......

eventMouseover: function(calEvent, domEvent) {
    if (typeof calEvent.id != 'undefined'){
        var layer = "<div id='events-layer' class='fc-transparent' style='position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100'> <a> <img border='0' style='padding-right:5px;' src='/icon_note_delete.png' width='16' onClick='deleteEvent("+calEvent.id+");'></a></div>";
        $(this).append(layer);
    }
}

不起作用的部分是

onClick='deleteEvent("+calEvent.id+");

因为 id 将是一个字符串而不是数字,我需要引用它。

它给了我

onClick="deleteEvent(e_1984_2_184_668)"

,但我需要

onClick="deleteEvent('e_1984_2_184_668')"

我已经尝试过

onClick='deleteEvent(\'"+calEvent.id+"\');

,但它也不起作用。我知道这是我所缺少的愚蠢的东西,但非常感谢您的帮助!

I know this should be basic but for the life of me....

eventMouseover: function(calEvent, domEvent) {
    if (typeof calEvent.id != 'undefined'){
        var layer = "<div id='events-layer' class='fc-transparent' style='position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100'> <a> <img border='0' style='padding-right:5px;' src='/icon_note_delete.png' width='16' onClick='deleteEvent("+calEvent.id+");'></a></div>";
        $(this).append(layer);
    }
}

The part that isn't working is

onClick='deleteEvent("+calEvent.id+");

Since the id will be a string not a number I need to quote it.

as is it is giving me

onClick="deleteEvent(e_1984_2_184_668)"

but I need

onClick="deleteEvent('e_1984_2_184_668')"

I have tried

onClick='deleteEvent(\'"+calEvent.id+"\');

but it didn't work either. I know this is something stupid that I am missing but help is much appriecated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀中猫帐中妖 2024-12-09 03:39:48

您应该使用双引号将 calEvent.id 括起来,因为它位于单引号字符串内:

var layer = "<div id='events-layer' class='fc-transparent' style='position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100'> <a> <img border='0' style='padding-right:5px;' src='/icon_note_delete.png' width='16' onClick='deleteEvent(\""+calEvent.id+"\");'></a></div>";

但是,在我看来,如果您使用一系列 jQuery 调用来创建您的HTML 改为:

var layer = $('<div></div>').attr('id', 'events-layer')
                .addClass('fc-transparent')
                .css({
                    position: 'absolute',
                    width: '100%',
                    height: '100%',
                    top: '-1px',
                    textAlign: 'right',
                    zIndex: '100'
                })
                .appendTo(this);

var anchor = $('<a></a>').appendTo(layer);

var img = $('<img>').attr('border', '0')
                    .attr('src', '/icon_note_delete.png')
                    .attr('width', '16')
                    .css({
                        paddingRight: '5px'                        
                    })
                    .click(function() { deleteEvent(calEvent.id); })
                    .appendTo(anchor);

You should use double-quotes to surround calEvent.id, because it's within a single quote string:

var layer = "<div id='events-layer' class='fc-transparent' style='position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100'> <a> <img border='0' style='padding-right:5px;' src='/icon_note_delete.png' width='16' onClick='deleteEvent(\""+calEvent.id+"\");'></a></div>";

However, it would be easier and more readable in my opinion if you used a chain of jQuery calls to create your HTML instead:

var layer = $('<div></div>').attr('id', 'events-layer')
                .addClass('fc-transparent')
                .css({
                    position: 'absolute',
                    width: '100%',
                    height: '100%',
                    top: '-1px',
                    textAlign: 'right',
                    zIndex: '100'
                })
                .appendTo(this);

var anchor = $('<a></a>').appendTo(layer);

var img = $('<img>').attr('border', '0')
                    .attr('src', '/icon_note_delete.png')
                    .attr('width', '16')
                    .css({
                        paddingRight: '5px'                        
                    })
                    .click(function() { deleteEvent(calEvent.id); })
                    .appendTo(anchor);
行至春深 2024-12-09 03:39:48

尝试一下:

 ... onClick='deleteEvent('" + calEvent.id + "'); ...

Try that:

 ... onClick='deleteEvent('" + calEvent.id + "'); ...
无言温柔 2024-12-09 03:39:48

翻转单引号和双引号可能会更容易(JSFiddle):

var calEventId = 'testid';

var layer = '<div id="events-layer" class="fc-transparent" style="position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100"> <a> <img border="0" style="padding-right:5px;" src="/icon_note_delete.png" width="16" onClick="deleteEvent(\'' + calEventId + '\');"></a></div>';

alert(layer);

然后你可以转义单引号,生成的 HTML 字符串看起来更像是您在日常标记中看到的 HTML(属性值用双引号,JavaScript 中用单引号)。

It might be easier to flip the single and double quotes around (JSFiddle):

var calEventId = 'testid';

var layer = '<div id="events-layer" class="fc-transparent" style="position:absolute; width:100%; height:100%; top:-1px; text-align:right; z-index:100"> <a> <img border="0" style="padding-right:5px;" src="/icon_note_delete.png" width="16" onClick="deleteEvent(\'' + calEventId + '\');"></a></div>';

alert(layer);

Then you can escape the single quotes, and the resulting HTML string looks more like that HTML you would see in everyday markup (double-quotes around attribute values, single-quotes in the JavaScript).

在巴黎塔顶看东京樱花 2024-12-09 03:39:48

看来你正在使用 jQuery。我建议使用 jQuery 创建元素:

$('<div />', {
     id: 'events-layer',
     'class': 'fc-transparent someClass',
}).append(
    $('<a />', {
         href: '<you need an href attribute>',
    }).append('<img />', {
        'class': 'someOtherClass',
        src: '/icon_note_delete.png',
        width: '16',
        click: function() {
            deleteEvent(calEvent.id);
        }
    })
).appendTo(this);

其中 someClasssomeOtherClass 为:

.someClass {
    position:absolute; 
    width:100%; 
    height:100%; 
    top:-1px; 
    text-align:right; 
    z-index:100
} 

.someOtherClass {
    border: none;
    padding-right:5px;
    width: 16px;
}

It seems you are using jQuery. I suggest to create the elements with jQuery:

$('<div />', {
     id: 'events-layer',
     'class': 'fc-transparent someClass',
}).append(
    $('<a />', {
         href: '<you need an href attribute>',
    }).append('<img />', {
        'class': 'someOtherClass',
        src: '/icon_note_delete.png',
        width: '16',
        click: function() {
            deleteEvent(calEvent.id);
        }
    })
).appendTo(this);

where someClass and someOtherClass would be:

.someClass {
    position:absolute; 
    width:100%; 
    height:100%; 
    top:-1px; 
    text-align:right; 
    z-index:100
} 

.someOtherClass {
    border: none;
    padding-right:5px;
    width: 16px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文