Javascript 单击事件在iOS中contenteditable div内的链接上不起作用
一切似乎都正常,但当我应用“单击”或“执行”时,任何其他事件都无法在具有contenteditable的iOS WKWebView中运行。我假设代码中没有错误。但我想我还需要做些我不知道的事情。 我需要帮助来克服这个问题。如果有任何诀窍或选择,请建议Javascript 单击事件在iOS中contenteditable div内的链接上不起作用,javascript,jquery,ios,wkwebview,Javascript,Jquery,Ios,Wkwebview,一切似乎都正常,但当我应用“单击”或“执行”时,任何其他事件都无法在具有contenteditable的iOS WKWebView中运行。我假设代码中没有错误。但我想我还需要做些我不知道的事情。 我需要帮助来克服这个问题。如果有任何诀窍或选择,请建议 import UIKit import WebKit class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate { //Outlets
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
//Outlets
@IBOutlet weak var containerViewForWKWebView : UIView! //Set background color to very light grey color.
//Variables
var webView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Initializing webView
webView = WKWebView()
//Setting delegate and other additional settings for webView
webView?.isUserInteractionEnabled = true
webView?.navigationDelegate = self
webView?.uiDelegate = self
webView?.scrollView.bounces = false
webView?.backgroundColor = UIColor.black //Setting webView background color to black.
//Now adding webView inside containerViewForWKWebView.
containerViewForWKWebView.addSubview(webView!)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Updating layout for webView.
let frame = CGRect(x: 0.0, y: 0.0, width: containerViewForWKWebView.bounds.width, height: containerViewForWKWebView.bounds.height)
webView?.frame = frame
view.layoutIfNeeded()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func webPageForCheckboxTest(_ sender: UIButton) {
loadLocalWebPageFromBundleMain(webPageFileName: "local-web-page-for-checkbox")
}
@IBAction func intertnetPageForCheckboxTest(_ sender: UIButton) {
let webSiteAddress = URL(string: "http://juntolife.com/for_testing/web-page-for-checkbox.html")
let myURLRequest = URLRequest(url: webSiteAddress!)
webView?.load(myURLRequest)
}
@IBAction func webPageForClickEventTest(_ sender: UIButton) {
loadLocalWebPageFromBundleMain(webPageFileName: "local-web-page-for-clickevent")
}
@IBAction func internetPageForClickEventTest(_ sender: UIButton) {
let webSiteAddress = URL(string: "http://juntolife.com/for_testing/web-page-for-clickevent.html")
let myURLRequest = URLRequest(url: webSiteAddress!)
webView?.load(myURLRequest)
}
func loadLocalWebPageFromBundleMain(webPageFileName: String) {
var htmlString: String!
let webPageURLFromBundleMain = Bundle.main.url(forResource: webPageFileName, withExtension: "html")
let baseURL = webPageURLFromBundleMain?.deletingLastPathComponent()
print("Base URL of Bundle Main: \(baseURL)")
do {
let fileContent = try String(contentsOf: webPageURLFromBundleMain!, encoding: String.Encoding.utf8)
htmlString = fileContent
} catch let error as NSError {
print("Failed getting content of the file: \(webPageURLFromBundleMain), Error: " + error.localizedDescription)
htmlString = ""
}
if htmlString != "" {
webView?.loadHTMLString(htmlString, baseURL: baseURL)
} else {
print("Local web page file is not found in bundle main.")
htmlString = "<!DOCTYPE html><html><body><h3>Local web page file is not found in bundle main.</h3></body></html>"
}
}
}
导入UIKit
导入WebKit
类ViewController:UIViewController、WKUIDelegate、WKNavigationDelegate{
//出口
@IBOutlet弱var containerViewForWKWebView:UIView!//将背景颜色设置为非常浅的灰色。
//变数
var webView:WKWebView?
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
//初始化webView
webView=WKWebView()
//为webView设置代理和其他附加设置
webView?.isUserInteractionEnabled=true
webView?.navigationDelegate=self
webView?.uiDelegate=self
webView?.scrollView.bounces=false
webView?.backgroundColor=UIColor.black//将webView背景色设置为黑色。
//现在在containerViewForWKWebView中添加webView。
containerViewForWKWebView.addSubview(webView!)
}
覆盖函数视图显示(u动画:Bool){
super.viewdide显示(动画)
//正在更新webView的布局。
设frame=CGRect(x:0.0,y:0.0,宽度:containerViewForWKWebView.bounds.width,高度:containerViewForWKWebView.bounds.height)
webView?.frame=frame
view.layoutIfNeeded()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
@iAction func webPageForCheckboxTest(u发件人:ui按钮){
loadLocalWebPageFromBundleMain(webPageFileName:“复选框的本地网页”)
}
@iAction func InternetPageForCheckBoxTest(u发送方:UIButton){
让webSiteAddress=URL(字符串:http://juntolife.com/for_testing/web-page-for-checkbox.html")
让myURLRequest=URLRequest(url:webSiteAddress!)
webView?.load(myURLRequest)
}
@iAction func网页用于ClickEventTest(u发件人:ui按钮){
loadLocalWebPageFromBundleMain(webPageFileName:“clickevent的本地网页”)
}
@iAction func internetPageForClickEventTest(u发件人:ui按钮){
让webSiteAddress=URL(字符串:http://juntolife.com/for_testing/web-page-for-clickevent.html")
让myURLRequest=URLRequest(url:webSiteAddress!)
webView?.load(myURLRequest)
}
func loadLocalWebPageFromBundleMain(webPageFileName:String){
var htmlString:字符串!
让webPageURLFromBundleMain=Bundle.main.url(forResource:webPageFileName,扩展名:“html”)
让baseURL=webPageURLFromBundleMain?.deletingLastPathComponent()
打印(“主捆绑包的基本URL:\(基本URL)”)
做{
让fileContent=try String(contentsOf:webgeurlfrombundlemain!,编码:String.encoding.utf8)
htmlString=fileContent
}将let错误捕获为NSError{
打印(“获取文件内容失败:\(webPageURLFromBundleMain),错误:“+Error.localizedDescription”)
htmlString=“”
}
如果htmlString!“”{
webView?.loadHTMLString(htmlString,baseURL:baseURL)
}否则{
打印(“在主捆绑包中找不到本地网页文件。”)
htmlString=“在主捆绑包中找不到本地网页文件。”
}
}
}
clickevent.html的本地网页
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Title</title>
</head>
<body style="background-color:#bce0ff;">
<div>
<h3 style="text-align: center;"><strong>Local Web Page For Click Event Test</strong></h3>
<p>Link outside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
</div>
<div contenteditable="true" id="content" style="background-color:#ffffff;">
<p>Link inside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
</div>
<script>
document.querySelector("body").addEventListener('click', function(e) {
var anchor = e.target.closest('a');
if(anchor !== null) {
anchor.innerHTML = Date();
}
}, false);
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Title</title>
</head>
<body style="background-color:#bce0ff;">
<div>
<h3 style="text-align: center;"><strong>Local Web Page For Check Box Test</strong></h3>
<p>Check box outside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
</div>
<div contenteditable="true" id="content" style="background-color:#ffffff;">
<p>Check box inside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
</div>
</body>
</html>
标题
点击事件测试的本地网页
链接外部内容可编辑:请单击它查看它是否工作
可编辑内容内的链接:请单击该链接查看其是否工作
document.querySelector(“正文”).addEventListener(“单击”),函数(e){
var anchor=e.target.closest('a');
如果(锚定!==null){
anchor.innerHTML=Date();
}
},假);
checkbox.html的本地网页
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Title</title>
</head>
<body style="background-color:#bce0ff;">
<div>
<h3 style="text-align: center;"><strong>Local Web Page For Click Event Test</strong></h3>
<p>Link outside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
</div>
<div contenteditable="true" id="content" style="background-color:#ffffff;">
<p>Link inside content editable: <a href="#"> This is a dummy link</a> Please click it to see it is working or not.</p>
</div>
<script>
document.querySelector("body").addEventListener('click', function(e) {
var anchor = e.target.closest('a');
if(anchor !== null) {
anchor.innerHTML = Date();
}
}, false);
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Title</title>
</head>
<body style="background-color:#bce0ff;">
<div>
<h3 style="text-align: center;"><strong>Local Web Page For Check Box Test</strong></h3>
<p>Check box outside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
</div>
<div contenteditable="true" id="content" style="background-color:#ffffff;">
<p>Check box inside content editable: <input type="checkbox" /> This is a checkbox input. Please check it to see it is working or not.</p>
</div>
</body>
</html>
标题
复选框测试的本地网页
内容可编辑外的复选框:这是一个复选框输入。请检查它是否正常工作
可编辑内容内的复选框:这是一个复选框输入。请检查它是否正常工作
您使用过Evernote吗?它正在contenteditable下开发他们的应用程序。不,我从来没有。在jQuery或Swift?w3中委托events?@Teemu:在3.0版中不推荐使用Delegate()方法。请改用on()方法。@Wolf我尝试了您的建议,但我需要连续内容编辑模式。我正在尝试构建一个富文本编辑器。所以我需要点击图片或复选框上的事件。但iOS不允许这样做。还有其他帮助吗?你用过Evernote吗?它正在contenteditable下开发他们的应用程序。不,我从来没有。在jQuery或Swift?w3中委托events?@Teemu:在3.0版中不推荐使用Delegate()方法。请改用on()方法。@Wolf我尝试了您的建议,但我需要连续内容编辑模式。我正在尝试构建一个富文本编辑器。所以我需要点击图片或复选框上的事件。但iOS不允许这样做。还有其他帮助吗?