我想创建一个带有contenteditable div的WYSIWYG HTML文本编辑器.
为此,我使用window.getSelection()来检索所选文本,当我单击一个按钮(按钮粗体)时,我执行一个函数来在所选文本周围添加粗体标签.
但是我对javascript代码(没有JQuery)没有任何想法来添加粗体标签.
这是我的代码:
<input type="button" value="B" style="font-weight:bold;" onclick='add_tags("b");'>
<div id="container_contenteditable" class="container_contenteditable" contenteditable></div>
<script type="text/javascript">
function add_tags(tags)
{
container_contenteditable = document.getElementById("container_contenteditable");
//Retrieve the selected text :
sel = window.getSelection();
}
</script>
Run Code Online (Sandbox Code Playgroud) 在 safari 5 中,不支持新音频,因此错误控制台显示:
TypeError : 'undefined' is not a constructor (evaluating 'new Audio')
Run Code Online (Sandbox Code Playgroud)
我如何以编程方式知道浏览器是否支持新音频?
我有一个带有 2 个提交按钮的表单。在表单标签上,我把 onsubmit="return foo();"
因此,当我单击两个提交按钮之一时,由于 IE、Chrome、Firefox 中的 document.activeElement,我可以检索提交元素。但是 document.activeElement 在 safari 中不起作用(document.activeElement 返回一个 body 元素而不是提交按钮元素)。
这是我的代码(您可以在https://jsfiddle.net/m6quo8rs/ 上尝试):
<form onsubmit="foo();">
<input type="submit" id="submit_button_1" value="Submit button 1">
<input type="submit" id="submit_button_2" value="Submit button 2">
</form>
<script type="text/javascript">
function foo()
{
//Retrieve the submit button clicked thanks to document.activeElement :
console.log(document.activeElement);
}
</script>
Run Code Online (Sandbox Code Playgroud)
此代码适用于 IE、Chrome、Firefox,但不适用于 Safari。
你有 safari 的解决方案吗?
我有一个网页,他的网址是http://wwww.example.com/category/door/
但我想的重定向(301)http://www.example.com/category/到http://www.example.com/category/door/因为内容http://www.example.com/ category /为空。
我的重写规则如下:
RedirectPermanent /category/ /category/door/
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为浏览器将http://www.example.com/category/重定向到http://www.example.com/category/door/door/door/door/door/door/door/door/门/无限。
一个主意 ?谢谢。
在我的代码中,我有一个 h1 容器,里面有 h1 元素,如下所示:
<div id="container_h1"><h1 id="h1">Title H1</h1></div>
Run Code Online (Sandbox Code Playgroud)
然后,我将一个事件侦听器附加到 h1 元素,以便在用户单击 h1 元素时提醒 h1 文本:
var h1 = document.getElementById("h1");
h1.addEventListener(
"click",
function()
{
alert(h1.innerHTML);
},
false
);
Run Code Online (Sandbox Code Playgroud)
然后,我有 2 个用于删除和插入 h1 元素的按钮,如下所示:
<input type="button" value="remove H1" onclick="remove_h1();">
<input type="button" value="insert H1" onclick="insert_h1();">
//container_h1 element :
var container_h1 = document.getElementById("container_h1");
//Remove h1 :
function remove_h1()
{
container_h1.innerHTML = "";
}
//Re-appear h1 :
function insert_h1()
{
container_h1.innerHTML = '<h1 id="h1">Title H1</h1>';
}
Run Code Online (Sandbox Code Playgroud)
问题 :
当我通过单击“删除 H1”按钮使 h1 元素消失,然后通过单击“插入 H1”按钮使 …