首页 \ 问答 \ 如何在内联创建对象时引用父级?(How to reference parent in inline creation of objects?)

如何在内联创建对象时引用父级?(How to reference parent in inline creation of objects?)

假设我有一个父/子关系ob对象并尝试内联创建一个父对象(我不确定这是正确的单词)。 是否可以在自己的创建代码中引用创建的父代?

Parent = new Parent(
{
    Name = "Parent",
    Child= new Child(/*ReferenceToParent*/)
});

Let's say I have a parent/child-relationship ob objects and try to create a parent object inline (I'm not really sure that's the right word). Is it possible to reference the created parent in its own creation code?

Parent = new Parent(
{
    Name = "Parent",
    Child= new Child(/*ReferenceToParent*/)
});

原文:https://stackoverflow.com/questions/6843923
更新时间:2021-09-10 14:09

最满意答案

这是你得到的问题。 CSS风格

@media only screen and (max-width:659px){}

将计算窗口屏幕的max-width ,包括scrollbar ,但是当你打电话时

$(window).width()

它将返回除scrollbar之外的窗口屏幕的宽度,当您的窗口屏幕(包括使用Chrome developer tools检查它的scrollbar )宽度介于659px676px之间时,会导致您出现意外结果。

您需要做的是使用CSS作为基础,通过在@media only screen and (max-width:659px){}添加一些style @media only screen and (max-width:659px){}来定义宽度是否> 659px ,在您的情况下我正在改变z-index3035来检测CSS是否已定义width已经< 660px

然后使用jQuery ,你需要检查类.barz-index CSS值是30 (表示宽度> 659px)还是35 (表示另一个),然后使用它作为条件而不是使用$(window).width()

以下是您的示例中的工作示例:

$(window).on('scroll', function () {
    var offset = $(window).scrollTop();
    if ($('.bar').css('z-index') == '30' && offset > 10) {
        $('#nav-bar').addClass('slideDown');
    } else {
        $('#nav-bar').removeClass('slideDown');
    }
});
ul li {
  display: inline-block;
}
.bar {
  top: -67px;
  width: 100%;
  height: 67px;
  opacity: 0.97;
  z-index: 30;
  position: fixed;
  border-bottom: solid 1px #2B292B;
  background-color: #FCFCFC;
  -webkit-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  -webkit-transition: top 0.4s;
  -moz-transition: top 0.4s;
  -ms-transition: top 0.4s;
  -o-transition: top 0.4s;
  transition: top 0.4s;
}
.slideDown {
  top: 0;
}
body {
  height: 1500px;
}
@media only screen and (max-width: 659px) {
  .bar {
    top: 0;
    z-index: 35;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div id="nav-bar" class="bar">
    <div id="nav-content" class="cf">
        <div id="branding-wrap"> <a href="homeurl"><img src="/images/logo.png" id="logo"/></a>

        </div>
        <!-- End #branding-wrap -->
        <nav id="main-nav" class="right">
            <ul>
                <li>menu-item</li>
                <li>menu-item</li>
                <li>menu-item</li>
            </ul>
        </nav> <a href="#mobile-nav"><img src="/images/menu.png" id="menu-icon" class="left"/></a>

    </div>
    <!-- End #nav-content -->
</div>
<!-- End #nav-bar -->

注意 :单击“整Full Page以便可以调整窗口宽度


Here's the problem that you get. The CSS style

@media only screen and (max-width:659px){}

will calculate the max-width of the window screen including the scrollbar, but when you call

$(window).width()

it will return the width of the window screen excluding the scrollbar, there what caused you to have unexpected results when your window screen (including the scrollbar if you check it using the Chrome developer tools) width is between 659px & 676px.

What you need to do is to use the CSS as your base to define if it width is > 659px or not by adding some style to the @media only screen and (max-width:659px){}, in your case I was changing the z-index from 30 to 35 to detect if the CSS has defined that the width is already < 660px

Then using the jQuery, you need to check whether the z-index CSS value of class .bar is 30 (which means that the width is > 659px) or 35 (which mean the other), then use it as the conditional if rather than using the $(window).width().

Here's the working example from your example:

$(window).on('scroll', function () {
    var offset = $(window).scrollTop();
    if ($('.bar').css('z-index') == '30' && offset > 10) {
        $('#nav-bar').addClass('slideDown');
    } else {
        $('#nav-bar').removeClass('slideDown');
    }
});
ul li {
  display: inline-block;
}
.bar {
  top: -67px;
  width: 100%;
  height: 67px;
  opacity: 0.97;
  z-index: 30;
  position: fixed;
  border-bottom: solid 1px #2B292B;
  background-color: #FCFCFC;
  -webkit-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
  -webkit-transition: top 0.4s;
  -moz-transition: top 0.4s;
  -ms-transition: top 0.4s;
  -o-transition: top 0.4s;
  transition: top 0.4s;
}
.slideDown {
  top: 0;
}
body {
  height: 1500px;
}
@media only screen and (max-width: 659px) {
  .bar {
    top: 0;
    z-index: 35;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div id="nav-bar" class="bar">
    <div id="nav-content" class="cf">
        <div id="branding-wrap"> <a href="homeurl"><img src="/images/logo.png" id="logo"/></a>

        </div>
        <!-- End #branding-wrap -->
        <nav id="main-nav" class="right">
            <ul>
                <li>menu-item</li>
                <li>menu-item</li>
                <li>menu-item</li>
            </ul>
        </nav> <a href="#mobile-nav"><img src="/images/menu.png" id="menu-icon" class="left"/></a>

    </div>
    <!-- End #nav-content -->
</div>
<!-- End #nav-bar -->

Note: Click the Full Page so you can resize the window width

相关问答

更多
  • 从您提供的示例开始,我进行了以下修改: 从HTML中删除了h1标记 将navigation的style属性值放入CSS 为div.menu ul元素添加了一些CSS 这是一个小提琴,包括所做的修改 笔记: JS代码没有被修改(它与你的例子的JS代码相同)。 使用JSFiddle时,不要忘记从“框架和扩展”列表中选择jQuery;) CSS: #navigation { background-color:#FFF; height:100%; width:250px; float ...
  • 你可以设置style="display:none;" 最初的列表 这里是更新jsfiddle https://jsfiddle.net/2mxo7zdq/2/ you can set the style="display:none;" for the list initially here is the updates jsfiddle https://jsfiddle.net/2mxo7zdq/2/
  • 你可以在这里使用z-index和一些jQuery实现你想要做的事情。 当openall按钮被激活时,您可以将sidebar的z-index设置为较高的值(例如9999 ,并且可以在隐藏侧边栏时将z-index设置为0 。 这些是我在代码中修改过的两个jQuery函数: $("#hide-nav").click(function() { $("#newcontent").animate({'left':"30px"}, 250); $(".sublinks").hide(250); $ ...
  • 我相信你的问题在.rslides1_nav的这一行: background: transparent url("themes.gif") no-repeat left top; 尝试: background: transparent url("http://responsiveslides.com/with-captions/themes.gif") no-repeat left top; 或将该图像复制到您的本地,并使用适当的链接。 我一直在用小提琴搞乱,但它看起来并不正确。 希望这至少可以让你开始 ...
  • 你可以使用css @media,这里是一个例子 @media (max-width:767px){ .class-name { width: 50% !important; } } max-width是此css将应用的最大屏幕宽度,您可以按最小宽度更改最大宽度并根据需要调整其值。 you can use css @media, here is an example @media (max-width:767px){ .class-name { width: 50% !imp ...
  • 这是你得到的问题。 CSS风格 @media only screen and (max-width:659px){} 将计算窗口屏幕的max-width ,包括scrollbar ,但是当你打电话时 $(window).width() 它将返回除scrollbar之外的窗口屏幕的宽度,当您的窗口屏幕(包括使用Chrome developer tools检查它的scrollbar )宽度介于659px和676px之间时,会导致您出现意外结果。 您需要做的是使用CSS作为基础,通过在@media only ...
  • 你可以找到id为'sunset'的幻灯片索引。 如果你的flexslider HTML结构就像..
    ......
    ......
    ......
    ......
    试试这个代码.. var index = $('#sunset').index(); // will ...
  • 你可以尝试几种选择。 如果要使用.animate(),则需要更改其初始状态的位置。 因此,在你的CSS中,你需要将你的div的初始状态设置为不在页面之外的东西,比如 #slide { position: absolute; right: 999em; } 使用此设置,添加.animate()将为指定的值设置指定属性的动画,因此请在动画完成后指定div最终的位置。 $("#slide").delay(5000).animate({right: 0}, 500); 另一种方法是使用jQuery ad ...
  • 因为你已经拥有你的div的id,所以为什么不尝试通过它的id滚动到特定的div。 我在这里解释了这个: http : //usmansidea.blogspot.com/2013/07/jquery-make-html-pages-scroll-able.html 你需要做的是将以下函数放在$ .document.ready(function(){})中; function scrolView(i) { $('div').each(function () { ...
  • 您正在调整窗口大小时添加单击事件。 这将多次将相同的函数绑定到click事件。 所以最好的选择是将事件绑定一次,这可以在就绪事件上完成。 在这个链接中,我通过向navi-btn添加一个类并检查它来限制resize事件本身的这个绑定。 $(".navi-btn").not(".binded").addClass("binded").click(function () { $(".navigation").slideToggle(); }); 希望这会给你一个想法。 谢谢, Jothi You are ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)