相关疑难解决方法(0)

将HTML字符串转换为DOM元素?

有没有办法转换HTML像:

<div>
<a href="#"></a>
<span></span>
</div>
Run Code Online (Sandbox Code Playgroud)

或任何其他HTML字符串到DOM元素?(这样我就可以使用appendChild())了.我知道我可以做.innerHTML和.innerText,但这不是我想要的 - 我真的希望能够将动态HTML字符串转换为DOM元素,以便我可以在.appendChild()中传递它.

更新:似乎有困惑.我有一个字符串中的HTML内容,作为JavaScript中变量的值.文档中没有HTML内容.

html javascript dom

179
推荐指数
5
解决办法
31万
查看次数

Stringify(转换为JSON)具有循环引用的JavaScript对象

我有一个包含循环引用的JavaScript对象定义:它有一个引用父对象的属性.

它还具有我不想传递给服务器的功能.我如何序列化和反序列化这些对象?

我读过这样做的最好方法是使用Douglas Crockford的stringify.但是,我在Chrome中收到以下错误:

TypeError:将循环结构转换为JSON

代码:

function finger(xid, xparent){
    this.id = xid;
    this.xparent;
    //other attributes
}

function arm(xid, xparent){
    this.id = xid;
    this.parent = xparent;
    this.fingers = [];

    //other attributes

    this.moveArm = function() {
        //moveArm function details - not included in this testcase
        alert("moveArm Executed");
    }
}

 function person(xid, xparent, xname){
    this.id = xid;
    this.parent = xparent;
    this.name = xname
    this.arms = []

    this.createArms = function () {
        this.arms[this.arms.length] = new arm(this.id, this);
    }
}

function group(xid, xparent){
    this.id = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json stringify

68
推荐指数
5
解决办法
8万
查看次数

如果有循环引用,如何将DOM节点序列化为JSON?

我想将DOM节点甚至整个序列化为windowJSON.

例如:

 >> serialize(document)
    -> {
      "URL": "http://stackoverflow.com/posts/2303713",
      "body": {
        "aLink": "",
        "attributes": [
          "getNamedItem": "function getNamedItem() { [native code] }",
          ...
        ],
        ...
        "ownerDocument": "#" // recursive link here
      },
      ...
    }
Run Code Online (Sandbox Code Playgroud)

JSON.stringify()

JSON.stringify(window) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)

问题是JSON默认不支持循环引用.

var obj = {}
obj.me = obj
JSON.stringify(obj) // TypeError: Converting circular structure to JSON
Run Code Online (Sandbox Code Playgroud)

window和DOM节点有很多.window === window.window将如此document.body.ownerDocument === document.

此外,JSON.stringify不序列化函数,所以这不是我想要的.

dojox.json.ref

 `dojox.json.ref.toJson()` can easily serialize object with …
Run Code Online (Sandbox Code Playgroud)

javascript serialization json dom circular-reference

48
推荐指数
3
解决办法
6万
查看次数

如何以编程方式读取wkwebview的控制台日志

我正在尝试以编程方式读取以我的WkWebview加载的webapp的控制台日志.

到目前为止在我的研究中它是不可能的.

任何帮助实现这一点将不胜感激!

javascript uiwebview ios swift wkwebview

15
推荐指数
7
解决办法
8220
查看次数

触摸事件不返回触摸数据

编辑:"这不能在Angular UI Modals中完成"是一个有效的答案,如果实际情况如此.

这是我从触摸事件中获得的返回数据.显然缺少任何有用的触摸X/Y坐标(https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches).这是一个毫无希望的通用问题,但是,任何想法?传递给触摸时执行的函数的"event"对象:

{
      "originalEvent": {
        "isTrusted": true
      },
      "type": "touchstart",
      "timeStamp": 1450388006795,
      "jQuery203026962137850932777": true,
      "which": 0,
      "view": "$WINDOW",
      "target": {},
      "shiftKey": false,
      "metaKey": false,
      "eventPhase": 3,
      "currentTarget": {},
      "ctrlKey": false,
      "cancelable": true,
      "bubbles": true,
      "altKey": false,
      "delegateTarget": {},
      "handleObj": {
        "type": "touchstart",
        "origType": "touchstart",
        "data": null,
        "guid": 2026,
        "namespace": ""
      },
      "data": null
    }
Run Code Online (Sandbox Code Playgroud)

现在,这是在画布中的角度UI模式,但鼠标事件工作正常.这是我的元素btw:

link: function(scope, element, attrs, model){
                //scope.canvasElem = element[0].children[0].children[0];
                scope.canvasElem = angular.element($('.touchScreen'))[0];
                scope.ctx = scope.canvasElem.getContext('2d');
Run Code Online (Sandbox Code Playgroud)

这是我如何绑定的一个例子:

element.bind('touchstart', scope.touchStart);
Run Code Online (Sandbox Code Playgroud)

编辑,这是一个用于比较的mousedown事件对象:

{
  "originalEvent": { …
Run Code Online (Sandbox Code Playgroud)

javascript touch angularjs jqlite

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

如何使用循环引用保存对象?

我想在本地保存一个具有循环引用的对象.我有什么选择?

我的第一个想法是使用HTML5本地存储,但由于循环引用,我无法对此对象进行字符串化.

具体来说,我正在尝试保存当前选择的DOMSelection对象.

例:

  var sel = window.getSelection();
  var selstring = JSON.stringify(sel); // Breaks here ...
  localStorage.setItem("selection",selstring);
Run Code Online (Sandbox Code Playgroud)

我现在可以让stringify工作的唯一方法是忽略某些对象,如下所示:

var selstring = JSON.stringify(sel,function(k,v){
    if( k=="anchorNode" ||
        k=="baseNode" ||
        k=="extentNode" ||
        k=="focusNode") return undefined;

    return v;
});
Run Code Online (Sandbox Code Playgroud)

但这给我留下了一个相当空的DOMSelection对象,这对我所需要的还不够.

有没有其他方法可以保存这个对象?唯一的要求是它在移动游猎中运行,其他任何东西都是真的.解决方案可以是javascript或jquery(或任何其他js lib,如果需要).

感谢您的任何帮助,您可以提供.

javascript serialization storage json circular-reference

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

为什么JSON.stringify只显示click事件的isTrusted成员?

HTML:

<button onclick="foo(event);">Test</button>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

window.foo = function(event) {
  console.log(JSON.stringify(event));
}
Run Code Online (Sandbox Code Playgroud)

控制台结果:

{"isTrusted":true}
Run Code Online (Sandbox Code Playgroud)

它发生在Chrome上.我还没有测试过其他浏览器.

javascript json dom-events

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

字符串化事件对象有效,但实际上是空的

我有这个自定义的stringify函数,可以处理循环引用:

const customStringify = function (v) {
  return JSON.stringify(v, function(k, v) {
    if (v instanceof Node) {
      return 'Node';
    }
    if (v instanceof Window) {
      return 'Window';
    }
    return v;
  });
};
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用它来对事件对象进行字符串化处理:

  window.addEventListener('click', function (ev) {   // MouseEvent
    const v = customStringify(ev);  // {"isTrusted":true}
  });
Run Code Online (Sandbox Code Playgroud)

v只是看起来像这样的字符串:

{"isTrusted":true}
Run Code Online (Sandbox Code Playgroud)

好奇怪 我尝试了其他一些自定义字符串化辅助函数,它们都给我相同的结果。

我看着这个线程: 如何对事件对象进行字符串化处理?

但我的问题似乎更具体。

javascript json dom

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