简体   繁体   English

C#似乎无法在JavaScript字符串中使用变量值

[英]C# can't seem to use variable value in javascript string

Not sure where I'm goign wrong here, so I'm trying to use two string in javascript, but visual studio is showing the strings as error. 不知道我在哪里错了,所以我试图在JavaScript中使用两个字符串,但是Visual Studio将字符串显示为错误。

The code: 编码:

@{
                            string animCat = string.Format("#animCat{0}", counter);
                            string animCatClone = string.Format("#animCatClone{0}", counter);
                        }
                        <script type="text/javascript">
                            if (!jQuery(@animCat)[0].beginElement) {
                                jQuery(".home-category-container .image-wrapper.clone, .home-category-container .image-wrapper.orig").css({ "filter": "blur(25px)" });
                            }

                            jQuery(document).ready(function () {
                                setTimeout(function () {
                                    if (jQuery(@animCat)[0].beginElement) {
                                        jQuery(@animCat + ", " + @animCatClone)[0].beginElement();
                                    }
                                    else {
                                        jQuery(".home-category-container .image-wrapper").css("filter", "blur(0px)");
                                    }
                                    jQuery(".home-category-container .image-wrapper.orig").css("visibility", "visible");
                                    jQuery(".home-category-container .image-wrapper.clone").remove();
                                }, 1000);
                            });
                        </script>

Image showing what I mean: 该图显示了我的意思:

在此处输入图片说明

Not sure what I'm missing. 不知道我在想什么。

Also when I hover over the red squiggle line it says "Predefined type 'System.String' is not defined or imported". 同样,当我将鼠标悬停在红色的花样线条上时,它会显示“未定义或导入'预定义类型'System.String''”。

Have also tried if (jQuery('#animCat' + @counter)[0].beginElement) {} but this doesn't work #animCat' + 1 etc is output Cheers 还尝试了if (jQuery('#animCat' + @counter)[0].beginElement) {}但这不起作用#animCat' + 1等输出欢呼声

Ok so it's now working, 好吧,现在可以了,

As Yeldar Kurmangaliyev suggested I needed the quotes, but single quotes not double '@animCat' 正如Yeldar Kurmangaliyev建议的那样,我需要引号,但单引号不能将'@animCat'加倍

This is the code that works: 这是有效的代码:

@{
                            string animCat = string.Format("#animCat{0}", counter);
                            string animCatClone = string.Format("#animCatClone{0}", counter);
                        }
                        <script type="text/javascript">
                            if (!jQuery('@animCat')[0].beginElement) {
                                jQuery(".home-category-container .image-wrapper.clone, .home-category-container .image-wrapper.orig").css({ "filter": "blur(25px)" });
                            }

                            jQuery(document).ready(function () {
                                setTimeout(function () {
                                    if (jQuery('@animCat')[0].beginElement) {
                                        jQuery('@animCat, @animCatClone')[0].beginElement();
                                    }
                                    else {
                                        jQuery(".home-category-container .image-wrapper").css("filter", "blur(0px)");
                                    }
                                    jQuery(".home-category-container .image-wrapper.orig").css("visibility", "visible");
                                    jQuery(".home-category-container .image-wrapper.clone").remove();
                                }, 1000);
                            });
                        </script>

This part of code still shows error red squiggle line: 这部分代码仍然显示错误的红色花线:

string animCat = string.Format("#animCat{0}", counter);
string animCatClone = string.Format("#animCatClone{0}", counter);

But when I build the solution and test it works now :) 但是,当我构建解决方案并测试它现在可以工作时:)

Cheers all for the help 为所有人加油打气

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM