I have a img element , his src attribute is a svg url file.
I can get this img dom element, but I want to get this file content, and can't use ajax.
thanks!
example:
<svg>
<rect> </rect>
</svg>
this is my code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img id="testImg" src="logo.svg" alt="">
<script>
const img = document.getElementById('testImg');
console.log('img', [img]);
</script>
</body>
</html>
This is my scene! I can't use ajax because I can't get the data of the asynchronous setting of 'ondragstart' in the 'ondrop' event.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<img id="img" draggable="true" src="logo.png" width="50px" height="50px" />
<div
id="div"
style="background-color: #123; width: 400px; height: 300px;"
></div>
<script>
const img = document.getElementById('img');
img.addEventListener('dragstart', function(e) {
const src = e.target.src;
console.log(src);
axios.get(src).then(function(res) {
console.log(res.data);
e.dataTransfer.setData('text/plain', res.data);
});
});
const div = document.getElementById('div');
div.addEventListener('dragover', function(e) {
e.preventDefault();
});
div.ondrop = function(e) {
const data = e.dataTransfer.getData('text/plain');
console.log(data);
};
</script>
</body>
</html>