event.prevent Default()機能しないピット(JSコピー選択されたテキストコードを含む)

24068 ワード

理由:JSはDOMの前に置いた.変えたら止められます.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Task Listtitle>
head>
<body>
<script src="jquery-3.2.1.min.js">script>
<script>
    var selectTxt ='';
    document.getElementsByTagName("body")[0].onmouseup = function () {
        selectTxt = window.getSelection().toString();
        selectTxt.length && $("#keyword_container").text(selectTxt);
    };
    $("#camel").click(function (e) {
        e.preventDefault();
    })
script>
<div id="tst">this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>
    <style scoped>
        .container_qj{
            width: 100%;
            height: 50px;
            position: fixed;
            background-color: #F6F6F5;
            bottom: 0;
        }
        #keyword_container{
            float: left;
            width: 300px;
            height: 40px;
            margin: 7px 0 3px 5px;
            background-color: cyan;
        }
        .hyperLinkContainer{
            float: left;
            list-style: none !important;
            height: 100%;
            margin: 0 0 0 20px;
            padding: 0;
        }
        .hyperLinkContainer li{
            list-style: none !important;
            float: left;
            margin-left: 5px;
            margin-top: 7px;
            background-color: aquamarine;
        }
        .hyperLinkContainer li a{
            text-decoration: none;
            display: block;
            height: 40px;
            line-height: 40px;
        }
    style>
    <div class="container_qj">
        <span id="keyword_container">span>
        <ul class="hyperLinkContainer">
            <li><a id="camel" href="" target='_blank'>camelcamelcamela>li>
            <li><a href="" target='_blank'>ebaya>li>
            <li><a href="" target='_blank'>googlea>li>
            <li><a href="" target='_blank'>amazona>li>
            <li><a href="" target='_blank'>alibabaa>li>
            <li><a href="" target='_blank'>aliexpressa>li>
        ul>
    div>
div>

body>
html>
上記のコードのように、preventDefaultは機能しません.
下は交換したものです.役に立つと思います.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Task Listtitle>
head>
<body>
<script src="jquery-3.2.1.min.js">script>
<div id="tst">this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>this is testdiv>
<div>
    <style scoped>
        .container_qj{
            width: 100%;
            height: 50px;
            position: fixed;
            background-color: #F6F6F5;
            bottom: 0;
        }
        #keyword_container{
            float: left;
            width: 300px;
            height: 40px;
            margin: 7px 0 3px 5px;
            background-color: cyan;
        }
        .hyperLinkContainer{
            float: left;
            list-style: none !important;
            height: 100%;
            margin: 0 0 0 20px;
            padding: 0;
        }
        .hyperLinkContainer li{
            list-style: none !important;
            float: left;
            margin-left: 5px;
            margin-top: 7px;
            background-color: aquamarine;
        }
        .hyperLinkContainer li a{
            text-decoration: none;
            display: block;
            height: 40px;
            line-height: 40px;
        }
    style>
    <div class="container_qj">
        <span id="keyword_container">span>
        <ul class="hyperLinkContainer">
            <li><a id="camel" href="" target='_blank'>camelcamelcamela>li>
            <li><a href="" target='_blank'>ebaya>li>
            <li><a href="" target='_blank'>googlea>li>
            <li><a href="" target='_blank'>amazona>li>
            <li><a href="" target='_blank'>alibabaa>li>
            <li><a href="" target='_blank'>aliexpressa>li>
        ul>
    div>
div>
<script>
    var selectTxt ='';
    document.getElementsByTagName("body")[0].onmouseup = function () {
        selectTxt = window.getSelection().toString();
        selectTxt.length && $("#keyword_container").text(selectTxt);
    };
    $("#camel").click(function (e) {
        e.preventDefault();
        console.log("1:"+$("#camel"));
        console.log("1:"+$(this));
        console.log("2;"+this);
    })
script>
body>
html>