Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在'style'中'replace'内容-其中'html'存储在变量中_Javascript_Jquery_Html_Regex - Fatal编程技术网

Javascript 无法在'style'中'replace'内容-其中'html'存储在变量中

Javascript 无法在'style'中'replace'内容-其中'html'存储在变量中,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我正在尝试更新样式html,方法是替换字符串,但不起作用。我无法获得最终更新的html 以下是我的尝试: var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>" html += $(html).html(function(_,h){ $(h).find('style').html().rep

我正在尝试更新
样式
html,方法是
替换
字符串,但不起作用。我无法获得最终更新的
html

以下是我的尝试:

var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>"

html += $(html).html(function(_,h){
  $(h).find('style').html().replace(/^\w+ (\.\w+)/gm,'$1');
});

console.log(html); //throws the error.
var html=“043BF83A8FB24A418DA4248840101D.cls_0{font:26px'Arial';}测试”
html+=$(html).html(函数uh){
$(h).find('style').html().replace(/^\w+(\。\w+)/gm,$1');
});
log(html)//抛出错误。
这对我来说很有效

var html = "<style>043BF83A8FB24A418DA4248840101D .cls_0 {font:26px 'Arial';}</style><div>testing</div>";

var $h = $('<div>'+html+'</div>');
$h.find('style').html(function(_,h){ return h.replace(/^\w+ (\.\w+)/gm,'$1'); });
var updated = $h.html();

console.log(updated);
var html=“043BF83A8FB24A418DA4248840101D.cls_0{font:26px'Arial';}测试”;
变量$h=$(''+html+'');
$h.find('style').html(函数({uu,h){返回h.replace(/^\w+(\.\w+)/gm,$1');});
var updated=$h.html();
控制台日志(更新);

谢谢大家

为什么要否决投票内容
html
,我从
server
获取。对于示例,我进行了硬编码。这是jQuery抛出的错误,而不是
console.log
。以数字开头的标记名?也许您需要检查服务器端代码,
043BF83A8FB24A418DA4248840101D
看起来不像标签。我知道,这不是一个元素。我的问题是选择
style
元素并替换我正在查找的内容。但是
style
(即
html
变量)的内容无效,jQuery无法解析它并抛出错误。如果要将HTML传递给
$()
,则它必须有效。是。我同意你的看法。。你能把这个作为同意的答案吗。。?可能把你的实现。这也许有道理。