简体   繁体   English

获取ckeditor中元素的ID

[英]Get id of element within ckeditor

Unfortunately I have to open a new question for this as I cannot yet add comments to replies (in foreign posts). 不幸的是,我不得不对此提出一个新的问题,因为我还不能在回复中添加评论(在国外帖子中)。

In this question ( Clicking CKEditor content to show alert ) Palec posted a solution to get onclick events within CKEditor that works very well for me but as I hardly know jQuery I would need to know how I can get the id of the element (in this case the id of a div) within CKEditor using the code below. 在这个问题( 单击CKEditor的内容以显示警报 )中,Palec发布了一种解决方案,用于在CKEditor中获取onclick事件,对我来说效果很好,但是由于我几乎不了解jQuery,所以我需要知道如何获取元素的ID(在此使用以下代码在CKEditor中设置div的ID)。

This is the piece of code that I'm talking about. 这是我正在谈论的代码。

CKEDITOR.on('instanceReady', function() {
    $('.cke_contents iframe').contents().click(function() {
        alert('Clicked!');
    });
});

Thanks for your help, Pascal 感谢您的帮助,Pascal

You can get the id of an element in native Javascript using 您可以使用来获取本机Javascript中元素的ID

element.id

and in jQuery using 并在jQuery中使用

$('element').attr('id')

Working Example: 工作示例:

 // Native Javascript var myDiv = document.getElementsByTagName('div')[0]; var myDivId = myDiv.id; window.alert('Native javascript has found the id of the <div> is "' + myDivId + '"'); // jQuery $(document).ready(function(){ alert('jQuery Library has found the id of the <div> is "' + $('div').attr('id') + '"'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="my-div"></div> 

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

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