小编Mos*_*ini的帖子

根据Javascript,DIV的宽度是什么,为什么?

根据DOM的Javscript,DIV的宽度没什么.

这是一个如此简单的小问题,但它很烦人.

<html>
    <head>
        <style>
            div#foot_wrapper{
                width:650px;
                height:20px;
                position:absolute;
                bottom:10px;
                background-color:#000000;
            }
        </style>
        <script>

            function align(div){
                                 alert(div.style.width); // ---------------- box pops up blank?
                div.style.left = (window.innerWidth/2) - (div.style.width/2);       
            }
        </script>

    </head>

    <body onload="align(document.getElementById('foot_wrapper'))" onresize="align(document.getElementById('foot_wrapper'))" >

        <div id="main">
        </div>

        <div id="foot_wrapper">
        </div>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

html javascript css

3
推荐指数
1
解决办法
572
查看次数

语法错误:Javascript中缺少指数

我一直在语法错误.每次运行我的Javscript时都会丢失指数.

<script type="text/javascript">

function randomendtime(len) {
 var chars = ["10.202", "11.121", "12.101", "13.111", "14.121", "15.097", "18.142", "20.926"];
 return chars[ Math.floor(Math.random() * chars.length)];
}

var 2et = randomendtime(1);

document.write('<script type="text/javascript" src="http://www.example.com/' + 2et + '"><\/script>');

</script>
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
2878
查看次数

使用数组更新JavaScript对象以供参考

我有一个JavaScript对象;

xml
-nutrition
--daily values
--food
---0
----fat=20g
----sodium=
---1
----fat=20g
----sodium=5mg
---2
----fat=20g
----sodium=5mg
-stores
--0
--1
Run Code Online (Sandbox Code Playgroud)

我也有一个动态生成的javascript数组

["xml", "nutrition", "food", 0]
Run Code Online (Sandbox Code Playgroud)

如何基于此数组更新javascript对象?无需手动输入

myobj[array[0]][array[1]][array[2]][array[3]].fat = '30g';
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
42
查看次数

如何在Javascript中用点替换逗号

我有一个带有多个选项的html select元素,当选定的javascript将显示特定的输入字段时。在这些字段中,我具有将逗号(,)更改为点(。)的javascript函数。

问题是,只有id为id的输入字段才#size可以使用,选择其他字段后,什么都不会改变。我正在使用jQuery。

这是JSFiddle中的完整代码

<select id="switch" name="switch">
  <option value="">Select...</option>
  <option value="size">Size</option>
  <option value="weight">Weight</option>
  <option value="dimensions">Dimensions</option>
</select>

<div class="switch-body">
<!-- javascript content goes here -->
</div>
Run Code Online (Sandbox Code Playgroud)

Javascript:

$(document).ready(function() {
    $("#switch").change(function() {
        //if selected option is size
        if ($(this).val() == "size") {
            $(".switch-body").html('<input type="text" id="size" placeholder="Product Size">');
        }
        //if selected option is weight
        else if ($(this).val() == "weight") {
            $(".switch-body").html('<input type="text" id="weight" placeholder="Product Weight">');
        }
        //if selected option is weight
        else if ($(this).val() == "dimensions") {
            $(".switch-body").html(`
            <input type="text" id="height" …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

3
推荐指数
1
解决办法
78
查看次数

从头开始动画 gif

我想使用 gif 图像制作鼠标效果,但问题是图像不是从头开始的。 html 代码:
<div style="width: 100px;height: 100px;background: red;" onclick="myFunction(this,event)"></div>
JavaScript 代码:

function myFunction(ele,e)
{
var x = e.clientX - 15;
var y = e.clientY - 15;
var effect = document.createElement("DIV");
effect.style.background="url('http://s12.postimg.org/piulwsk61/image.gif')";
effect.style.backgroundSize="100% 100%";
effect.style.width="75px";
effect.style.height="75px";
effect.style.zIndex="1";
effect.style.position="fixed";
effect.style.top=y;
effect.style.left=x;
ele.appendChild(effect);
setTimeout(function(){ele.removeChild(effect);},700);
}
Run Code Online (Sandbox Code Playgroud)

问题已解决:
javascript代码:

function myFunction(ele,e)
{
var x = e.clientX - 15;
var y = e.clientY - 15;
var effect = document.createElement("IMG");
effect.style.src="http://s12.postimg.org/piulwsk61/image.gif";
effect.style.width="75px";
effect.style.height="75px";
effect.style.zIndex="1";
effect.style.position="fixed";
effect.style.top=y;
effect.style.left=x;
ele.appendChild(effect);
setTimeout(function(){ele.removeChild(effect);},700);
}
Run Code Online (Sandbox Code Playgroud)

javascript animated-gif

2
推荐指数
1
解决办法
2466
查看次数

前端如何处理标准Error对象?

我有一个带有许多API请求的前端应用程序,但是使用错误响应很痛苦。

有时我需要遍历许多嵌套对象,例如:error.response.data.email有时是error.response.data.address.province[0]

我无法预测所有错误,因此编写手动“解析器”对我来说似乎是一个肮脏的额外解决方案:

const errorsObject = err.response.data
    let errors = ''

    Object.keys(errorsObject).map(key => {
      if (key === 'address') {
        Object.keys(errorsObject.address).map(
          key => (errors += `${key} : ${errorsObject.address[key]}\n`)
        )
      } else {
        errors += `${key} : ${errorsObject[key]}\n`
      }
      return undefined
    })

    yield toast.error(errors)
Run Code Online (Sandbox Code Playgroud)

而且它仍然不能涵盖所有内容。

是否有任何前端解析器?如果没有,我们的后端是Python(Django),也许后端有一个软件包?理想情况下,我希望看到一系列平面的对象{title, message}

javascript api error-handling

2
推荐指数
1
解决办法
184
查看次数

在点击事件监听器中使用时,这不是一个功能

我正在处理javascript文件,并具有以下代码

class test(){
    constructor(){
    }
    showBox(){
        var countBlock = document.createElement("div");
        document.body.appendChild(countBlock); 
        countBlock.addEventListener('click', function(){
            this.showList()
        });
    }
    showList(){
        console.log('Element clicked')
    }
}
Run Code Online (Sandbox Code Playgroud)

除非单击元素,否则代码工作正常,当我单击它时,它显示 this.showList() is not a function

不确定如何解决

javascript

1
推荐指数
1
解决办法
53
查看次数

标签 统计

javascript ×7

animated-gif ×1

api ×1

css ×1

error-handling ×1

html ×1

jquery ×1