如何使用Word 2016的Javascript API在ContentControl上实现OnEnter和OnExit事件 [英] How to implement OnEnter and OnExit event on ContentControl using Javascript API for Word 2016

查看:113
本文介绍了如何使用Word 2016的Javascript API在ContentControl上实现OnEnter和OnExit事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中有很多contentcontrols,我需要找出一种将光标置于内容控件中的方式,以便我选择该控件并相应地进行操作.我认为通过为contentcontrols实现onEnter和onExit事件,我可以实现它.但是我不知道如何在JavaScript api中声明和调用这些事件处理程序.任何帮助,我们都感激不尽.

There are many contentcontrols in a document and I need to find out a way that the cursor is in which content control so that I will select that control and do the operation accordingly. I think by implementing onEnter and onExit events for contentcontrols , I can achieve it. But I don't know how to declare and invoke those eventhandlers in JavaScript api. Any help is really appreciated.

推荐答案

好问题!我们确实有一个用于内容控件的onEnter事件(我们将其称为binding.selectionChanged.我们还有一个binding.dataChanged事件,如果用户更改内容并退出内容控件,则会触发该事件

Good question! We do have an onEnter event for content controls (we call it binding.selectionChanged. We also have a binding.dataChanged event who gets triggered if the user changes the content and exits the content control

迈克尔提出的另一种解决方案是为文档中的每个内容控件创建绑定,然后注册此类事件.

so an alternative solution to what Michael proposed is to create bindings for each content control in the document and then register for such events.

您可以通过以下方法实现此目的: 1.遍历内容控件集合.(使用 body.contentControls 集合) 2.对于每个内容控件,抓住或设置标题,然后使用其按命名项创建绑定.检查 bindings.addFromNamedItem 方法. 3.在CallBack上,确保预订绑定的selectionChanged(或DataChanged). 创建绑定代码并注册事件将如下所示:

you can achieve this by: 1. traversing the content control collection.(use body.contentControls collection) 2. for each content control, grab or set the title and use it to create a binding by named item. check the bindings.addFromNamedItem method. 3. on the callBack make sure to subscribe to the selectionChanged (or DataChanged) for the binding. the create binding code and register to the events will look like this:

function CreateCCSelectionChangedEvent() {
        Office.context.document.bindings.addFromNamedItemAsync("TitleOfTheContentControl", { id: 'Binding01' }, function (result) {
            if (result.status == 'succeeded') {
                result.value.addHandlerAsync(Office.EventType.BindingSelectionChanged, handler);
            }
        });
       
    }

    function handler() {
       console.log("Event Triggered!");
    }

希望这会有所帮助!

这篇关于如何使用Word 2016的Javascript API在ContentControl上实现OnEnter和OnExit事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆