gpt4 book ai didi

objective-c - 将 DOM 元素添加到 Cocoa WebView

转载 作者:行者123 更新时间:2023-12-03 17:21:55 26 4
gpt4 key购买 nike

我正在尝试使用 native Objective C 将表格添加到 Cocoa 中的 webview。

我创建了一个新的 cocoa 应用程序并向窗口添加了一个 webview。

-(id) initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self setEditable:YES];
self.UIDelegate = self;
}
return self;
}

-(void) webView:(WebView *)sender mouseDidMoveOverElement:(NSDictionary *)elementInformation modifierFlags:(NSUInteger)modifierFlags {
// NSLog(@"mouse over works");
}

-(void)webView:(WebView *)webView willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:(id<NSDraggingInfo>)draggingInfo
{
NSLog(@"Dragging also works");
[self insertTableInWebView];
}

-(void)insertTableInWebView
{
DOMDocument *domDocument = [[self mainFrame] DOMDocument];
DOMHTMLElement *tableElement = (DOMHTMLElement *)[domDocument createElement:@"div"];

NSString *tableString = @"<table> <tr> <td>This is a row </td </tr> </table>";
tableElement.innerHTML = tableString;

[self replaceSelectionWithNode:tableElement];
[self setNeedsDisplay:YES];
}

目前,这没有任何作用。我该如何解决这个问题?

最佳答案

尝试这样的事情

-(void)appendTagToBody:(NSString *)tagName InnerHTML:(NSString *)innerHTML
{


@try {
// Gets a list of all <body></body> nodes.

DOMNodeList *bodyNodeList = [[[self mainFrame] DOMDocument] getElementsByTagName:@"body"];

// There should be just one in valid HTML, so get the first DOMElement.
DOMHTMLElement *bodyNode = (DOMHTMLElement *) [bodyNodeList item:0];

// Create a new element, with a tag name.
DOMHTMLElement *newNode = (DOMHTMLElement *) [[[self mainFrame] DOMDocument] createElement:tagName];

// Add the innerHTML for the new element.
[newNode setInnerHTML:innerHTML];

// Add the new element to the bodyNode as the last child.
[bodyNode appendChild:newNode];


[newNode scrollIntoView:YES];


}
@catch (NSException *exception) {

}
}

此外,如果您想附加特定于 div,可能需要将 getElementsByTagName 更改为 getElementsById

它对我有用。

关于objective-c - 将 DOM 元素添加到 Cocoa WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com